2013-07-23 10 views
2

我不想讓鼠標中鍵在我的QTextEdit中粘貼文本。此代碼不起作用。 TextEdit繼承QTextEdit。鼠標中間按鈕粘貼後,粘貼複製的文本。如何禁用QTextEdit的中間按鈕功能?

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

我不能重現這一點,中間點擊不會粘貼任何文本給我。 – sashoalm

+2

另外,平臺Linux有沒有機會? – TheDarkKnight

+0

@ Merlin069根據'QTextControlPrivate :: mouseReleaseEvent'的源代碼,似乎中間的點擊也會粘貼到其他平臺上。 – alexisdm

回答

2

由於釋放按鈕時,鼠標點擊通常註冊,你應當重新定義mouseReleaseEvent功能。

你甚至不需要重新定義mousePressEvent,因爲中間按鈕根本沒有被該函數處理。

1

我假設你在這裏使用Linux;在你處理鼠標事件之前,右鍵單擊窗口很可能會觸發MIME數據的插入,這就是爲什麼它仍在粘貼文本。

因此,根據Qt文檔粘貼: - 「修改QTextEdit可以粘貼什麼以及如何粘貼,重新實現虛擬canInsertFromMimeData()和insertFromMimeData()函數。」

+0

重新定義'insertFromMimeData'也會改變拖放的方式,而不是隻改變中間按鈕的行爲。 – alexisdm

+0

@alexisdm重新定義它並檢查鼠標中鍵是否被按下,否則調用基類函數將提供所需的行爲。 – TheDarkKnight

+0

我不說它不會工作,我只是說在一個函數中添加鼠標處理代碼,應該避免與鼠標沒有任何關係。 – alexisdm

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

}