1 #include "MyWidget.h" 2 #include <QApplication> 3 #include <QEvent> 4 #include <QDebug> 5 #include <QMouseEvent>//子类 6 #include <QKeyEvent> 7 #include <QVBoxLayout> 8 9 MyWidget::MyWidget(QWidget *parent) : QWidget(parent) 10 { 11 QVBoxLayout* lay = new QVBoxLayout(this); 12 #if 0 13 QPushButton* button2; 14 lay->addWidget(button = new QPushButton("OK", this)); 15 lay->addWidget(button2 = new QPushButton("Button2")); 16 //按钮接收键盘信息,而不仅是鼠标 17 button->setDefault(true);//空格键 18 this->setMouseTracking(true);//鼠标不需要按住,mouseMove就能得到调用 19 20 connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));//单击,接收信号 21 connect(button2, SIGNAL(clicked()), this, SLOT(slotButtonClicked())); 22 #endif 23 lay->addWidget(edit = new QLineEdit()); 24 connect(edit, SIGNAL(returnPressed()),this ,SLOT(slotButtonClicked())); 25 } 26 void MyWidget::slotButtonClicked()//槽函数 27 { 28 //QPushButton* button = (QPushButton*)sender(); 29 QLineEdit* button = (QLineEdit*)sender(); 30 qDebug() << button->text();//可以知道那个按钮被操作 31 } 32 /* 任何信息都是QApplication先得到-》具体应该处理的窗口::event()->event()根据信息类型来调用具体的虚函数*/ 33 /* 1)可以重载具体的虚函数,来实现对消息的响应 34 * 2)可以重载event函数,用来处理或者截取消息*/ 35 /*截取消息*/ 36 bool MyWidget::event(QEvent *ev) //重载event函数 37 { 38 /* 39 if (ev->type() == QEvent::MouseButtonPress) // 40 { 41 QMouseEvent* ev1 = (QMouseEvent*)ev; //强制类型转换QEvent 转 QMouseEvent 42 qDebug() << "mouse press"; 43 } 44 */ 45 //if(ev->type() == QEvent::MouseButtonPress)//截断 46 // return true; 47 ev->accept(); 48 49 return QWidget::event(ev);//父类的虚函数 50 } 51 52 void MyWidget::closeEvent(QCloseEvent *)//关闭窗口后发送 53 { 54 qDebug() << "closeEvent"; 55 } 56 57 void MyWidget::mousePressEvent(QMouseEvent *ev) 58 { 59 #if 0 60 QPoint pt = ev->pos(); 61 qDebug() << pt; 62 if (ev->button() == Qt::LeftButton) 63 { 64 65 } 66 if (ev->modifiers() == Qt::ShiftModifier) 67 { 68 qDebug() << "shift press"; 69 } 70 #endif 71 if(ev->button() == Qt::LeftButton)//左键 72 { 73 if (ev->modifiers()==Qt::ControlModifier)//ctrl 74 { 75 return; 76 } 77 } 78 else 79 {} 80 } 81 void MyWidget::mouseReleaseEvent(QMouseEvent *) 82 {} 83 void MyWidget::mouseMoveEvent(QMouseEvent *) 84 { 85 static int i = 0; 86 qDebug() << "mouse move"<<i++; 87 } 88 void MyWidget::keyPressEvent(QKeyEvent *ev) 89 { 90 ev->modifiers(); 91 int key = ev->key(); 92 qDebug() << key; 93 char a = key; 94 qDebug() << (char)a; 95 } 96 void MyWidget::keyReleaseEvent(QKeyEvent *) 97 {} 98 int main(int argc, char* argv[]) 99 { 100 QApplication app(argc, argv); 101 MyWidget w; 102 w.show(); 103 104 return app.exec(); 105 }
1 #ifndef MYWIDGET_H 2 #define MYWIDGET_H 3 4 #include <QWidget> 5 #include <QPushButton> 6 #include <QLineEdit> 7 class MyWidget : public QWidget 8 { 9 Q_OBJECT 10 public: 11 explicit MyWidget(QWidget *parent = nullptr); 12 13 /* widget's event handler (all event) */ 14 bool event(QEvent *);//函数原型 15 16 void mousePressEvent(QMouseEvent *); 17 void mouseReleaseEvent(QMouseEvent *); 18 void mouseMoveEvent(QMouseEvent *); 19 /*不要用*/ 20 //void mouseDoubleClickEvent(QMouseEvent *); 21 22 void keyPressEvent(QKeyEvent *); 23 void keyReleaseEvent(QKeyEvent *); 24 25 void closeEvent(QCloseEvent *); 26 27 //void showEvent(QShowEvent *); 28 //void hideEvent(QHideEvent *); 29 30 QPushButton* button; 31 QLineEdit* edit; 32 33 signals: 34 35 public slots: 36 void slotButtonClicked(); 37 }; 38 39 #endif // MYWIDGET_H
HEADERS += MyWidget.h SOURCES += MyWidget.cpp QT += widgets gui
一部分关于按钮消息,另一部分关于文本输入。