QT中事件机制及对应的Demo
QT的事件机制类别
根据对Qt事件机制的分析, 我们可以得到5种级别的事件过滤,处理办法. 以功能从弱到强, 排列如下:
1)重载特定事件处理函数.
最常见的事件处理办法就是重载象mousePressEvent(), keyPressEvent(), paintEvent() 这样的特定事件处理函数.
2)重载event()函数.
通过重载event()函数,我们可以在事件被特定的事件处理函数处理之前(象keyPressEvent())处理它. 比如, 当我们想改变tab键的默认动作时,一般要重载这个函数. 在处理一些不常见的事件(比如:LayoutDirectionChange)时,evnet()也很有用,因为这些函数没有相应的特定事件处理函数. 当我们重载event()函数时, 需要调用父类的event()函数来处理我们不需要处理或是不清楚如何处理的事件.
3) 在Qt对象上安装事件过滤器.
安装事件过滤器有两个步骤: (假设要用A来监视过滤B的事件)
首先调用B的installEventFilter( const QOject *obj ), 以A的指针作为参数. 这样所有发往B的事件都将先由A的eventFilter()处理.
然后, A要重载QObject::eventFilter()函数, 在eventFilter() 中书写对事件进行处理的代码.
4) 给QAppliction对象安装事件过滤器.
一旦我们给qApp(每个程序中唯一的QApplication对象)装上过滤器,那么所有的事件在发往任何其他的过滤器时,都要先经过当前这个 eventFilter(). 在debug的时候,这个办法就非常有用, 也常常被用来处理失效了的widget的鼠标事件,通常这些事件会被QApplication::notify()丢掉. ( 在QApplication::notify() 中, 是先调用qApp的过滤器, 再对事件进行分析, 以决定是否合并或丢弃)
5) 继承QApplication类,并重载notify()函数.
Qt 是用QApplication::notify()函数来分发事件的.想要在任何事件过滤器查看任何事件之前先得到这些事件,重载这个函数是唯一的办法. 通常来说事件过滤器更好用一些, 因为不需要去继承QApplication类. 而且可以给QApplication对象安装任意个数的事件。
这里我们假设有这样的业务需求:假设在一个弹窗中,需要监视用户在输入用户名和密码时,是否按下tab键来进行输入框的光标跳转:
方式一:重载event函数
protected:
bool event(QEvent *event) override;
在logindialog.c中的重载该函数:
bool logindialog::event(QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Tab) {
if (ui->usernameLineEdit->hasFocus()) {
ui->passwordLineEdit->setFocus();
return true;
} else if (ui->passwordLineEdit->hasFocus()) {
ui->loginPushButton->setFocus();
return true;
}
}
}
return QDialog::event(event);
}
方式二:事件过滤器
在Qt对象上安装事件过滤器.
安装事件过滤器有两个步骤: (假设要用A来监视过滤B的事件)
首先调用B的installEventFilter( const QOject *obj ), 以A的指针作为参数. 这样所有发往B的事件都将先由A的eventFilter()处理.
然后, A要重载QObject::eventFilter()函数, 在eventFilter() 中书写对事件进行处理的代码.
直接给demo:
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
在logindialog.c中的构造函数中安装事件过滤器:
logindialog::logindialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::logindialog)
{
ui->setupUi(this);
setWindowTitle("System Setting");
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::WindowCloseButtonHint;
setWindowFlags(flags);
ui->usernameLineEdit->setPlaceholderText("Username");
ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
ui->passwordLineEdit->setPlaceholderText("Password");
ui->usernameLineEdit->installEventFilter(this);
ui->passwordLineEdit->installEventFilter(this);
}
在logindialog.c中重载事件过滤器函数:
bool logindialog::eventFilter(QObject *obj,QEvent *event){
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (obj == ui->usernameLineEdit && keyEvent->key() == Qt::Key_Tab) {
ui->passwordLineEdit->setFocus();
return true;
}
if (obj == ui->passwordLineEdit && keyEvent->key() == Qt::Key_Tab) {
ui->loginPushButton->setFocus();
return true;
}
}
return QDialog::eventFilter(obj, event);
}
这样我们就能实现,当用户输入完用户名后,按下Tab键,就能跳转到密码输入框,输入完密码后,按下Tab键,鼠标光标就能自动聚焦于登录按钮上。