QtExamples/QtSigSlot/mainwindow.cpp
2021-06-21 23:44:17 +08:00

46 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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();
}