2

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

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

3 回答 3

2

由于鼠标点击通常是在释放按钮时注册的,因此您应该重新定义该mouseReleaseEvent函数。

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

于 2013-07-23T13:55:24.333 回答
1

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

因此,根据用于粘贴的 Qt 文档:-“要修改 QTextEdit 可以粘贴的内容以及粘贴方式,请重新实现虚拟 canInsertFromMimeData() 和 insertFromMimeData() 函数。”

于 2013-07-23T13:58:25.723 回答
0

我也遇到过同样的情况,也就是说:我CustomQTextEdit需要的部分是non-editable

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

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

}
于 2014-06-30T13:17:39.517 回答