0

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

1 回答 1

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);
}
于 2013-08-01T19:19:02.540 回答