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

How to write filtered keys to a subclassed QTextEdit screen?

本文关键字:子类 QTextEdit 屏幕 密钥 过滤      更新时间:2023-10-16

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

使用::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 + "rn>> ";
    }else{
        qstr = qstr + e->key();
    }
    ::TextEdit::setText(qstr);
}