2013-07-23 2 views
2

QTextEdit에 텍스트를 붙여 넣기 위해 마우스 가운데 버튼을 사용하고 싶지 않습니다. 이 코드는 작동하지 않습니다. TextEditQTextEdit을 상속합니다. 마우스 가운데 버튼을 붙여 넣으면 복사 된 텍스트를 붙여 넣습니다. 버튼을 놓을 때 마우스 클릭은 일반적으로 등록되기 때문에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 docs에 따라 붙여 넣기 : - "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())); 
      } 
     } 
    } 

} 
관련 문제