#ifndef TEST_H
#define TEST_H
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
class test;
}
class test : public QMainWindow
{
Q_OBJECT
public:
explicit test(QWidget *parent = 0);
~test();
private slots:
void on_button_clicked();
private:
Ui::test *ui;
QPushButton *button = nullptr;
};
#include "test.h"
#include "ui_test.h"
#include <QDebug>
test::test(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::test)
{
ui->setupUi(this);
button = new QPushButton(this); // 创建按钮
button->setObjectName("button"); // 指定按钮的对象
button->setText(QApplication::translate("test", "myButton", Q_NULLPTR));
QMetaObject::connectSlotsByName(this);
qDebug() << children(); // 输出所有子部件的列表
}
void test::on_button_clicked()
{
qDebug("signal and solt connected automatic\n");
}
1. ui->setupUi(this); 一定要放在前面,
2.button创建后,一定要指定button的 Object Name
3.objectName 设置的什么名字,槽函数为on_(ObjectNmae)_clicked();格式
4.button属性设置完毕后 一定要调用 QMetaObject::connectSlotsByName(this);
Searches recursively for all child objects of the givenobject, and connects matching signals from them to slots ofobject that follow the following form: