Pregunta

I don't want mouse middle button to paste text in my QTextEdit. This code doesn't work. TextEdit inherits QTextEdit. After mouse middle button pastes it pastes copied text.

void TextEdit::mousePressEvent ( QMouseEvent * e ) {
    if (e->button() == Qt::MidButton) {
        e->accept();
        return;
    };
    QTextEdit::mousePressEvent(e);
}
¿Fue útil?

Solución

As mouse clicks are usually registered when the button is released, you should redefine the mouseReleaseEvent function.

You don't even need to redefine mousePressEvent, because the middle button isn't handled at all by that function.

Otros consejos

I'm assuming you're using Linux here; right clicking in the window is likely to be triggering an insertion of mime data before you get to handle the mouse event, which is why it is still pasting text.

Therefore, according to Qt docs for paste: - " to modify what QTextEdit can paste and how it is being pasted, reimplement the virtual canInsertFromMimeData() and insertFromMimeData() functions."

I've been in the same case, that is to say: having parts of my CustomQTextEdit required to be non-editable.

As I truly love the middle mouse button paste feature, I did not wanted to disable it. So, here is the (more or less quick and dirty coded) workaround I used:

void QTextEditHighlighter::mouseReleaseEvent(QMouseEvent *e)
{

    QString prev_text;
    if (e->button() == Qt::MidButton) {
        // Backup the text as it is before middle button click
        prev_text = this->toPlainText();
        // And let the paste operation occure...
        //        e->accept();
        //        return;
    }

    // !!!!
    QTextEdit::mouseReleaseEvent(e);
    // !!!!

    if (e->button() == Qt::MidButton) {

        /*
         * Keep track of the editbale ranges (up to you).
         * My way is a single one range inbetween the unique
         * tags "//# BEGIN_EDIT" and "//# END_EDIT"...
         */
        QRegExp begin_regexp = QRegExp("(^|\n)(\\s)*//# BEGIN_EDIT[^\n]*(?=\n|$)");
        QRegExp end_regexp  = QRegExp("(^|\n)(\\s)*//# END_EDIT[^\n]*(?=\n|$)");

        QTextCursor from = QTextCursor(this->document());
        from.movePosition(QTextCursor::Start);

        QTextCursor cursor_begin = this->document()->find(begin_regexp, from);
        QTextCursor cursor_end = this->document()->find(end_regexp, from);
        cursor_begin.movePosition(QTextCursor::EndOfBlock);
        cursor_end.movePosition(QTextCursor::StartOfBlock);
        int begin_pos = cursor_begin.position();
        int end_pos = cursor_end.position();

        if (!(cursor_begin.isNull() || cursor_end.isNull())) {
            // Deduce the insertion index by finding the position
            // of the first character that changed between previous
            // text and the current "after-paste" text
            int insert_pos; //, end_insert_pos;
            std::string s_cur = this->toPlainText().toStdString();
            std::string s_prev = prev_text.toStdString();

            int i_max = std::min(s_cur.length(), s_prev.length());
            for (insert_pos=0; insert_pos < i_max; insert_pos++) {
                if (s_cur[insert_pos] != s_prev[insert_pos])
                    break;
            }
            // If the insertion point is not in my editable area: just restore the 
            // text as it was before the paste occured
            if (insert_pos < begin_pos+1 || insert_pos > end_pos) {
                // Restore text (ghostly)
                ((MainWindow *)this->topLevelWidget())->disconnect(this, SIGNAL(textChanged()), ((MainWindow *)this->topLevelWidget()), SLOT(on_textEdit_CustomMacro_textChanged()));
                this->setText(prev_text);
                ((MainWindow *)this->topLevelWidget())->connect(this, SIGNAL(textChanged()), ((MainWindow *)this->topLevelWidget()), SLOT(on_textEdit_CustomMacro_textChanged()));
            }
        }
    }

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top