From 39bc540b04a8d25587eb85f389a77ee5e7c843ad Mon Sep 17 00:00:00 2001 From: xmuli Date: Sat, 26 Jun 2021 18:51:46 +0800 Subject: [PATCH] feat: QtConnect --- QtConnect/QtConnect.pro | 21 ++++++++++++++++++++ QtConnect/main.cpp | 11 +++++++++++ QtConnect/widget.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ QtConnect/widget.h | 14 ++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 QtConnect/QtConnect.pro create mode 100644 QtConnect/main.cpp create mode 100644 QtConnect/widget.cpp create mode 100644 QtConnect/widget.h diff --git a/QtConnect/QtConnect.pro b/QtConnect/QtConnect.pro new file mode 100644 index 0000000..6e664c6 --- /dev/null +++ b/QtConnect/QtConnect.pro @@ -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 diff --git a/QtConnect/main.cpp b/QtConnect/main.cpp new file mode 100644 index 0000000..b0a4ec2 --- /dev/null +++ b/QtConnect/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + return a.exec(); +} diff --git a/QtConnect/widget.cpp b/QtConnect/widget.cpp new file mode 100644 index 0000000..371b7a3 --- /dev/null +++ b/QtConnect/widget.cpp @@ -0,0 +1,43 @@ +#include "widget.h" +#include +#include + + +////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() +{ +} + diff --git a/QtConnect/widget.h b/QtConnect/widget.h new file mode 100644 index 0000000..47b6270 --- /dev/null +++ b/QtConnect/widget.h @@ -0,0 +1,14 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); +}; +#endif // WIDGET_H