如何将过滤后的密钥写入子类QTextEdit屏幕?

时间:2013-08-01 18:54:55

标签: c++ qt

如何将过滤后的密钥写入子类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  
    }
}

1 个答案:

答案 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);
}