1、子类化基类为QProgressDialog,例如为:MyProgressDialog,并重载even事件,头文件h:
#ifndef MYPROGRESSDIALOG_H
#define MYPROGRESSDIALOG_H
#include <QProgressDialog>
#include <QObject>
#include <QWidget>
#include <QtDebug>
#include <QKeyEvent>
class MyProgressDialog : public QProgressDialog
{
Q_OBJECT
public:
MyProgressDialog(QWidget *parent = 0);
bool event(QEvent *event);
};
#endif // MYPROGRESSDIALOG_H
2、子类cpp实现:(这里仅屏蔽Esc,空格键类似,自己添加即可)
#include "myprogressdialog.h"
MyProgressDialog::MyProgressDialog(QWidget *parent) :QProgressDialog(parent)
{
}
bool MyProgressDialog::event(QEvent *event) // 事件
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent && keyEvent->key() == Qt::Key_Escape)
{
qDebug() <<keyEvent->text();
qDebug() <<event->type();
keyEvent->accept();
return true;
}
return QProgressDialog::event(event);
}
3、调用处:
MyProgressDialog *dlg=new MyProgressDialog(this); //加不加this均可
dlg->reset(); //Qt5版本的QProgressDialog,创建后会自动弹出,reset一下即可解决
dlg->show();
4、当按下Esc时,为下面内容:
"\u001B"
QEvent::ShortcutOverride
"\u001B"
QEvent::KeyPress
"\u001B"
QEvent::KeyRelease
“\u001B”就是27,即Esc的键值。由此看出,按键Esc首先触发的是“QEvent::ShortcutOverride”,因此,不能仅简单的处理KeyPress事件。
参考:
1、c++ – Why does QEvent::ShortcutOverride event occur? – Stack Overflow22
2、
how to disable ESC key “close” the QProgressDialog ? | Qt Forum
版权声明:本文为dijkstar原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。