信号槽
1.类似windows的消息机制
2.信号函数,只发送不需要知道接受者
3.槽函数(普通函数),只接受不管通信
4.QObject绑定
Qt信号槽原理
1.绑定信号函数和槽函数
2.调用信号函数(将信号写入队列)
3.主线程从队列中获取信号
信号处理的主循环
QApplication a(argc,argv);
//Enters the main event loop and waits until exit() is called
return a.exec()
创建testsign.pro QWidget 项目
xz@xiaqiu:~/study/csdn/day0/testsign$ ls
build main.cpp testsign.pro widget.cpp widget.h widget.ui
xz@xiaqiu:~/study/csdn/day0/testsign$
打开设计器
xz@xiaqiu:~/study/csdn/day0/testsign$ designer
build/ main.cpp testsign.pro widget.cpp widget.h widget.ui
xz@xiaqiu:~/study/csdn/day0/testsign$ designer widget.ui
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QPushButton" name="CloseButton">
<property name="geometry">
<rect>
<x>100</x>
<y>160</y>
<width>241</width>
<height>141</height>
</rect>
</property>
<property name="text">
<string>关闭窗口</string>
</property>
</widget>
<widget class="QPushButton" name="minButton">
<property name="geometry">
<rect>
<x>460</x>
<y>160</y>
<width>241</width>
<height>141</height>
</rect>
</property>
<property name="text">
<string>最小化</string>
</property>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>CloseButton</sender>
<signal>clicked()</signal>
<receiver>Widget</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>244</x>
<y>235</y>
</hint>
<hint type="destinationlabel">
<x>305</x>
<y>615</y>
</hint>
</hints>
</connection>
<connection>
<sender>minButton</sender>
<signal>clicked()</signal>
<receiver>Widget</receiver>
<slot>showMinimized()</slot>
<hints>
<hint type="sourcelabel">
<x>580</x>
<y>230</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>299</y>
</hint>
</hints>
</connection>
</connections>
</ui>
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QPushButton *CloseButton;
QPushButton *minButton;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
CloseButton = new QPushButton(Widget);
CloseButton->setObjectName(QString::fromUtf8("CloseButton"));
CloseButton->setGeometry(QRect(100, 160, 241, 141));
minButton = new QPushButton(Widget);
minButton->setObjectName(QString::fromUtf8("minButton"));
minButton->setGeometry(QRect(460, 160, 241, 141));
retranslateUi(Widget);
QObject::connect(CloseButton, &QPushButton::clicked, Widget, qOverload<>(&QWidget::close));
QObject::connect(minButton, &QPushButton::clicked, Widget, qOverload<>(&QWidget::showMinimized));
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
CloseButton->setText(QCoreApplication::translate("Widget", "\345\205\263\351\227\255\347\252\227\345\217\243", nullptr));
minButton->setText(QCoreApplication::translate("Widget", "\346\234\200\345\260\217\345\214\226", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H