【发布时间】:2013-11-12 18:33:26
【问题描述】:
我正在尝试使用 keyPressEvent,但它仅在窗口具有焦点而不是任何 QWidget 时才有效。
这是我的代码:
在 customdialog.h 中:
class CustomDialog : public QDialog, public Ui::CustomDialog
{
Q_OBJECT
private:
Ui::CustomDialog *ui;
QString lastKey;
public:
CustomDialog(QWidget * parent = 0);
protected:
void keyPressEvent(QKeyEvent *e);
};
在 customdialog.cpp 中:
void CustomDialog::keyPressEvent(QKeyEvent *e)
{
lastKey = e->text();
qDebug() << lastKey;
}
如何使此类中的所有小部件都使用相同的 keyPressEvent?
【问题讨论】: