Chapter 1 Getting Started / Making Connections
signal/slot机制
本小节讲述的是QT特有的signal/slot机制。
代码如下:
1: #include <QApplication>
2: #include <QPushButton>
3:
4: int main(int argc, char *argv[])
5: {
6: QApplication app(argc, argv);
7: // QPushButton *button = new QPushButton("Quit");
8: // QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
9: // button->show();
10:
11: QPushButton button("Quit");
12: QObject::connect(&button, SIGNAL(clicked()), &app, SLOT(quit()));
13: button.show();
14:
15: return app.exec();
16: }
其中,第12行是事件绑定和处理的核心代码。
QPushButton提供的signal
Class | SIGNAL | 说明 |
QPushButton | 没有 | |
QAbstractButton | clicked pressed released toggled | 点击鼠标或者按下快捷键 按钮被按下 按钮被松开 按钮状态变化 |
QWidget | customContextMenuRequested | |
QObject | destroyed |
QApplication提供的SLOT
Class | SLOT | 说明 |
QApplication | aboutQt autoMaximizeThreshold autoSipEnabled closeAllWindows setAutoautoMaximizeThreshold setAutoSipEnabled setStyleSheet | only for Windows CE only for Windows CE only for Windows CE only for Windows CE |
QCoreApplication | quit | |
QObject | deleteLater |
后续工作
写个例子来使用pressed/released/toggled信号