如何禁用QTextEdit的中键功能?

时间:2013-07-23 13:40:12

标签: c++ linux qt qtextedit

我不希望鼠标中键在我的QTextEdit中粘贴文本。此代码不起作用。 TextEdit继承QTextEdit。在鼠标中键粘贴后,它会粘贴复制的文本。

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

3 个答案:

答案 0 :(得分:2)

由于在释放按钮时通常会注册鼠标点击,因此您应重新定义mouseReleaseEvent功能。

您甚至不需要重新定义mousePressEvent,因为该功能根本不处理中间按钮。

答案 1 :(得分:1)

我假设你在这里使用Linux;在你处理鼠标事件之前,右键单击窗口可能会触发插入mime数据,这就是它仍然粘贴文本的原因。

因此,根据Qt docs for paste: - “修改QTextEdit可以粘贴的内容及其粘贴方式,重新实现虚拟canInsertFromMimeData()和insertFromMimeData()函数。”

答案 2 :(得分:0)

我处于相同的情况,也就是说:让CustomQTextEdit的部分内容需要不可编辑

由于我真的很喜欢鼠标中键粘贴功能,所以我不想禁用它。所以,这里是我使用的(或多或少的快速和脏编码)解决方法:

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()));
            }
        }
    }

}