关于Qt 信号与槽自动连接 qPushbutton按钮无法点击的问题

#ifndef TEST_H
#define TEST_H
 
  
#include 
#include 
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 
 
  
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:

 
 

你可能感兴趣的:(QT,信号与槽自动连接)