如何将过滤后的密钥写入子类QTextEdit屏幕?
#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QWidget>
#include <QTextEdit>
#include <QMessageBox>
#include <QKeyEvent>
class TextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit TextEdit(QString qstr, QWidget *parent = 0);
QString m_qstr;
protected:
virtual void keyPressEvent(QKeyEvent *e);
signals:
public slots:
};
#endif // TEXTEDIT_H
#include "textedit.h"
TextEdit::JDLTextEdit(QString _qstr, QWidget *parent) :
QTextEdit(_qstr, parent)
{
}
void TextEdit::keyPressEvent(QKeyEvent *e)
{
if(e->key() == 16777220){ //"enterKey"
QString qstr = QString::number(e->key(), 'd',0);
QMessageBox *msgBox = new QMessageBox(0);
msgBox->setGeometry(QRect(QPoint(200,200),QSize(400,400)));
msgBox->setInformativeText(qstr);
msgBox->exec();
}else{
//write key chars to the QTextEdit screen
}
}
答案 0 :(得分:1)
使用::TextEdit::setText(qstr);
void TextEdit::keyPressEvent(QKeyEvent *e)
{
QString qstr = ::TextEdit::toPlainText();
//if(e->type() == QEvent::KeyPress){
if(e->key() == 16777220){
if(0){
QString qstr = QString::number(e->key(), 'd',0);
QMessageBox *msgBox = new QMessageBox(0);
msgBox->setGeometry(QRect(QPoint(200,200),QSize(400,400)));
msgBox->setInformativeText(qstr);
msgBox->exec();
}
qstr = qstr + "\r\n>> ";
}else{
qstr = qstr + e->key();
}
::TextEdit::setText(qstr);
}