2011-07-04 77 views

回答

2

你或許應該使用QTextEdit,因爲這是我們正在討論的豐富文本。

替代virtual void keyPressEvent (QKeyEvent * e)。您可以在實施中致電QTextEdit::keyPressEvent委託非特殊情況。

0

實際上,您可以使用帶有eventFilter和installEventFilter函數的對象。

#ifndef SHIFTENTERFILTER_H 
#define SHIFTENTERFILTER_H 

#include <QObject> 
#include <QEvent> 
#include <QKeyEvent> 

class ShiftEnterFilter : public QObject 
{ 
    protected: 
     virtual bool eventFilter(QObject *, QEvent *event) { 
      if(event -> type() == QEvent::KeyPress) 
      { 
       QKeyEvent *keyEvent = static_cast <QKeyEvent> (event); 

       if((keyEvent -> modifiers() & Qt::ShiftModifier) && ((keyEvent -> key() == Qt::Key_Enter) || (keyEvent -> key() == Qt::Key_Return))) 
        return true; 
      } 

      return false; 
     }  
    public: 
     ShiftEnterFilter(QObject *parent = 0) : QObject(parent) {} 
}; 

#endif 

此過濾器只需安裝到您的QPlainTextEdit

// code 
ui -> plainTextEdit -> installEventFilter(new ShiftEnterFilter(this)); 
// code