qfedu-qt-level/qtdemo03/form8.cpp

56 lines
1.3 KiB
C++
Raw Normal View History

#include "form8.h"
#include "ui_form8.h"
Form8::Form8(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form8)
{
ui->setupUi(this);
ui->Form8label->setWordWrap(true);
// ui->Form8label->setMinimumWidth(400);
// ui->Form8label->setMaximumWidth(400);
}
Form8::~Form8()
{
delete ui;
}
bool Form8::event(QEvent *evt)
{
qDebug()<<"事件分发"<<evt->type();
2023-08-10 12:05:02 +08:00
if(evt->type() == QEvent::KeyRelease){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(evt);
2023-08-10 12:05:02 +08:00
if(keyEvent->key() == Qt::Key_Tab||keyEvent->key() == Qt::Key_O){
// ui->Form8label->setText(tr("按下 %1 键").arg(keyEvent->text()));
qDebug()<<tr("按下 %1 键").arg(keyEvent->text());
2023-08-10 12:05:02 +08:00
return true;
}
// return false; // 不分发此事件
}
2023-08-10 12:05:02 +08:00
// return QWidget::event(evt);
return false;
}
void Form8::keyPressEvent(QKeyEvent *event)
{
QString s = ui->Form8label->text();
// 如果按下的是换行
if(event->key() == Qt::Key_Enter){
s+="\r\n";
}else if(event->key() == Qt::Key_Space){
s.clear();
}else if(event->key() == Qt::Key_Backspace){
// s.remove(s.size()-1,1);
s.chop(1);
}
else{
s += event->text();
}
ui->Form8label->setText(s);
}