qfedu-qt-level/qtdemo03/form8.cpp

54 lines
1.2 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();
if(evt->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(evt);
if(keyEvent->key() == Qt::Key_Return){
// ui->Form8label->setText(tr("按下 %1 键").arg(keyEvent->text()));
qDebug()<<tr("按下 %1 键").arg(keyEvent->text());
}
// return false; // 不分发此事件
}
return QWidget::event(evt);
}
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);
}