#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <iostream>
using namespace std;
//类声明
class Widget : public QWidget{
public:
bool event(QEvent *e);
};
class Button : public QPushButton{
public:
bool event(QEvent *e);
};
//类的实现
bool Widget::event(QEvent *e){
if(e->type() == QEvent::KeyPress)
cout << objectName().toStdString() << " keyDown" << endl;
if(e->type() == QEvent::MouseButtonPress)
cout << objectName().toStdString() << " mouseDown" << endl;
if(e->type() == QEvent::MouseButtonRelease)
cout << objectName().toStdString() << " mouseRelease" << endl;
return QWidget::event(e);
}
bool Button::event(QEvent *e){
if(e->type() == QEvent::KeyPress)
cout << objectName().toStdString() << " keyDown" << endl;
if(e->type() == QEvent::MouseButtonPress)
{
cout << objectName().toStdString() << " mouseDown" << endl;
return 1;
}
return QWidget::event(e);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
Button *b = new Button();
b->setParent(&w);
b->setText("testButton");
b->setObjectName("b");
w.setObjectName("w");
w.resize(333, 222);
w.show();
return a.exec();
}
【QT学习】事件传递
最新推荐文章于 2024-06-15 19:33:50 发布