feat: QtConnect
This commit is contained in:
parent
ee6b9252ba
commit
39bc540b04
21
QtConnect/QtConnect.pro
Normal file
21
QtConnect/QtConnect.pro
Normal file
@ -0,0 +1,21 @@
|
||||
QT += core gui
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
CONFIG += c++11
|
||||
|
||||
# You can make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
widget.cpp
|
||||
|
||||
HEADERS += \
|
||||
widget.h
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
11
QtConnect/main.cpp
Normal file
11
QtConnect/main.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include "widget.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
Widget w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
43
QtConnect/widget.cpp
Normal file
43
QtConnect/widget.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "widget.h"
|
||||
#include <QMetaMethod>
|
||||
#include <QPushButton>
|
||||
|
||||
|
||||
////Qt4: 宏
|
||||
///*式1*/ static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
|
||||
///*式2*/ QMetaObject::Connection connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const
|
||||
//// Qt5: 函数指针
|
||||
///*式3*/ static QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
|
||||
///*式4*/ static QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
|
||||
///*式5*/ static QMetaObject :: Connection QObject :: connect(const QObject * sender, const QMetaMethod&signal,const QObject * receiver, const QMetaMethod& method,Qt :: ConnectionType type = Qt :: AutoConnection)
|
||||
|
||||
|
||||
Widget::Widget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
setWindowTitle(tr("信号和槽:Qt4 与 Qt5 的 Connect 用法"));
|
||||
QPushButton *btn = new QPushButton(tr("关闭"), this);
|
||||
btn->move(width() / 2, height() / 2);
|
||||
setFixedSize(800, 600);
|
||||
|
||||
// Qt4: 宏
|
||||
// connect(btn, SIGNAL(clicked()), this, SLOT(close())); // 式 1
|
||||
// connect(btn, SIGNAL(clicked()), SLOT(close())); // 式 2
|
||||
|
||||
|
||||
// Qt5: 函数指针
|
||||
// connect(btn, &QPushButton::clicked, this, &Widget::close); // 式 3
|
||||
|
||||
// connect(btn, &QPushButton::clicked, [=](){ // 式 4
|
||||
// close();
|
||||
// });
|
||||
|
||||
int indexSig = btn->metaObject()->indexOfSignal("clicked(bool)");
|
||||
int indexSlot = metaObject()->indexOfSlot("close()");
|
||||
connect(btn, btn->metaObject()->method(indexSig), this, metaObject()->method(indexSlot)); // 式 5
|
||||
}
|
||||
|
||||
Widget::~Widget()
|
||||
{
|
||||
}
|
||||
|
14
QtConnect/widget.h
Normal file
14
QtConnect/widget.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef WIDGET_H
|
||||
#define WIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class Widget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Widget(QWidget *parent = nullptr);
|
||||
~Widget();
|
||||
};
|
||||
#endif // WIDGET_H
|
Loading…
Reference in New Issue
Block a user