首页 > 技术文章 > Qt如何重写虚函数

judes 2017-07-05 19:08 原文

eg:QWidget的有个虚函数,KeyPressEvent,当它的子类获得焦点的时候,如果有任何按键按下,就会触发这个虚函数。

1、在mainwindow.h中声明此虚函数

protected:void KeyPressEvent(QKeyEvent *event);

2、在mainwindow.cpp中重定义此虚函数

void MainWindow::keyPressEvent(QKeyEvent *event)

{

  qDebug()<<"12123";

}

推荐阅读