2017-03-08 6 views

답변

2


감사를 확인하는 시작하는 방법을 모른다 QPlainTextEdit에서 상속받은 클래스에서 event() 메서드를 다시 구현하고 마우스 추적 사용 가능 plaintextedit.cpp

#ifndef PLAINTEXTEDIT_H 
#define PLAINTEXTEDIT_H 

#include <QPlainTextEdit> 

class PlainTextEdit : public QPlainTextEdit 
{ 
public: 
    PlainTextEdit(QWidget *parent=0); 

    bool event(QEvent *event); 
}; 

#endif // PLAINTEXTEDIT_H 

plaintextedit.h

setMouseTracking()

#include "plaintextedit.h" 
#include <QToolTip> 


PlainTextEdit::PlainTextEdit(QWidget *parent):QPlainTextEdit(parent) 
{ 
    setMouseTracking(true); 
} 

bool PlainTextEdit::event(QEvent *event) 
{ 
    if (event->type() == QEvent::ToolTip) 
    { 
     QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event); 
     QTextCursor cursor = cursorForPosition(helpEvent->pos()); 
     cursor.select(QTextCursor::WordUnderCursor); 
     if (!cursor.selectedText().isEmpty()) 
      QToolTip::showText(helpEvent->globalPos(), /*your text*/QString("%1 %2").arg(cursor.selectedText()).arg(cursor.selectedText().length())); 

     else 
      QToolTip::hideText(); 
     return true; 
    } 
    return QPlainTextEdit::event(event); 
} 

전체 코드 : Here

+0

이 정말 도움이 답변을 주셔서 감사하지만 어떤 방법으로이 그냥 단어 강조 표시를 선택 하시겠습니까? 옵션이있는 경우 cursor.select (QTextCursor :: Highlight); 또는 그와 비슷한 것 –