feat: Q_OBJECT

This commit is contained in:
xmuli
2021-06-21 23:41:49 +08:00
parent f9e224204d
commit cac25277a0
9 changed files with 260 additions and 0 deletions

24
QtSigSlot/QtSigSlot.pro Normal file
View File

@@ -0,0 +1,24 @@
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 \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

11
QtSigSlot/main.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

45
QtSigSlot/mainwindow.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMetaMethod>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(tr("信号和槽Qt4 与 Qt5 用法"));
ui->pushButton->setText(tr("关闭窗口"));
// Qt4 方式
// connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(closeWindow())); // 式1
// connect(ui->pushButton, SIGNAL(clicked()), SLOT(closeWindow())); // 式2
// Qt5 方式
connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::closeWindow); // 式3
// connect(ui->pushButton, &QPushButton::clicked, [=](){ // 式4
// close();
// });
// connect(ui->pushButton, ui->pushButton->metaObject()->method(34), this, this->metaObject()->method(25)); // 式5
// for (int var = 0; var < metaObject()->methodCount(); ++var) {
// QMetaMethod qm1 = ui->pushButton->metaObject()->method(var);
// QMetaMethod qm2 = this->metaObject()->method(var);
// qDebug()<<qm1.name() << qm2.name() << var <<"\n";
// }
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeWindow()
{
close();
}

24
QtSigSlot/mainwindow.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void closeWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

45
QtSigSlot/mainwindow.ui Normal file
View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>240</x>
<y>110</y>
<width>141</width>
<height>71</height>
</rect>
</property>
<property name="text">
<string>关闭窗口</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>