将QPushButton :: clicked信号连接到插槽时出错

时间:2018-05-31 16:53:04

标签: c++ qt

首先,我使用for循环创建了大约20个按钮。并使用if else循环命名它们。现在,我想将每个按钮与新对话框连接起来。如果我使用了QT的设计模式,当我按下这样的connect(ui->pushButton_0, SIGNAl(released()), SLOT(digit_pressed())时,它会显示按钮的名称。但是,我不知道我作为for和if else循环制作的按钮的名称。 connect(ui-> .......)也没有显示任何预测。如何链接这些按钮和新对话框?

这是我的代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

private slots:
    void on_pushButton_clicked();

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QVBoxLayout>
#include "amputation.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->)



    QPixmap pix("/home/skanda/Desktop/D4564.png");
    ui->label_2->setPixmap(pix);
    setWindowTitle("First Aid");

    QVBoxLayout *lay = new QVBoxLayout();
    int i;
    for(i=0;i<25;i++)
    {
        if(i==0){
             QPushButton *button = new QPushButton("Amputation");
             lay->addWidget(button);
        }
        else if(i==1){
            QPushButton *button = new QPushButton("Asthama");
            lay->addWidget(button);

        }
        else if(i==2){
            QPushButton *button = new QPushButton("Bleeding");
            lay->addWidget(button);

        }
        else if(i==3){
            QPushButton *button = new QPushButton("Burns");
            lay->addWidget(button);

        }
        else if(i==4){
            QPushButton *button = new QPushButton("Chest Pain");
            lay->addWidget(button);

        }
        else if(i==5){
            QPushButton *button = new QPushButton("Diarrhoea");
            lay->addWidget(button);

        }
        else if(i==6){
            QPushButton *button = new QPushButton("Drowning");
            lay->addWidget(button);

        }
        else if(i==7){
            QPushButton *button = new QPushButton("Epilepsy");
            lay->addWidget(button);

        }
        else if(i==8){
            QPushButton *button = new QPushButton("Fainting");
            lay->addWidget(button);

        }
        else if(i==9){
            QPushButton *button = new QPushButton("Fever");
            lay->addWidget(button);

        }
        else if(i==10){
            QPushButton *button = new QPushButton("Food Poisoning");
            lay->addWidget(button);

        }
        else if(i==11){
            QPushButton *button = new QPushButton("Fracture");
            lay->addWidget(button);

        }
        else if(i==12){
            QPushButton *button = new QPushButton("Head Injury");
            lay->addWidget(button);

        }
        else if(i==13){
            QPushButton *button = new QPushButton("Muscle Strain");
            lay->addWidget(button);

        }
        else if(i==14){
            QPushButton *button = new QPushButton("No breathing");
            lay->addWidget(button);

        }
        else if(i==15){
            QPushButton *button = new QPushButton("Nose bleed");
            lay->addWidget(button);

        }
        else if(i==16){
            QPushButton *button = new QPushButton("Poisoning");
            lay->addWidget(button);

        }
        else if(i==17){
            QPushButton *button = new QPushButton("Snake Bites");
            lay->addWidget(button);

        }
        else if(i==18){
            QPushButton *button = new QPushButton("Stroke");
            lay->addWidget(button);

        }
        else if(i==19) {
            QPushButton *button = new QPushButton("Sun Burn");
            lay->addWidget(button);

        }
        else if(i==20) {
            QPushButton *button = new QPushButton("Testicle Pain");
            lay->addWidget(button);

        }
        else if(i==21) {
            QPushButton *button = new QPushButton("Ulcer");
            lay->addWidget(button);

        }
        else if(i==22) {
            QPushButton *button = new QPushButton("Child delievery");
            lay->addWidget(button);

        }
        else if(i==23) {
            QPushButton *button = new QPushButton("Heart Attack");
            lay->addWidget(button);

        }
        else {
            QPushButton *button = new QPushButton("Gastric");
            lay->addWidget(button);

        }
    }


    ui->scrollContents->setLayout(lay);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked(){
    Amputation amp;
    amp.setModal(true);
    amp.show();
}

**在这些代码中,我通过创建on_pushButton_clicked()函数尝试了我的运气。但是,这只是一次尝试。 **

2 个答案:

答案 0 :(得分:1)

在编程中,一切都在烹饪,:),让我们看看admin.auth().getUserByPhoneNumber(phoneNumber) .then(function(userRecord) { // User found. }) .catch(function(error) { console.log("Error fetching user data:", error); }); 的成分是什么:

connect()

所以发件人是connect(sender, &Sender::signal, receiver, &Receiver::slot); ,信号是buttons,接收者本身就是clicked,广告位this

我看到不必要的if-else,一切都可以简化为for:

on_pushButton_clicked

注意:

避免使用旧式连接,有许多缺点,请阅读以下链接的内容以获取更多信息:

答案 1 :(得分:0)

  1. 创建一个类的所有QPushButton成员,以便正确创建和销毁它们,而不是在循环中创建它们。

  2. 将每个人作为一个单独的成员后,请使用方法InitializeConnections()并使用Qt connect语法在其下建立所有连接。

  3. 如果您认为这些按钮是MainWindow课程的一部分,那么您的课程可能如下所示:

    class MainWindow{
        ...
        ...
        private :
          // Will make connections of each button to it's respective slot.
          void InitializeConnections();
    
        private :
          QPushButton *mAmputationButton;
          QPushButton *mAsthmaButton;
        //.. so on
    };
    

    MainWindow.cpp

    #include <QPushButton>
    #include <QVBoxLayout>
    
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mAmputationButton(new QPushButton("Amputation") ),
    mAsthmaButton(new QPushButton("Asthama") )
    {
       InitializeConnections();
    }
    
    void MainWindow::InitializeConnections()
    {
       connect(mAmputationButton, &QPushButton::clicked, this, &MainWindow::amputation_slot );
       connect(mAsthmaButton, &QPushButton::clicked, this, &MainWindow::asthma_slot );
      // same way for others.
    }
    

    我提到的插槽只是例如,将它连接到您需要连接的插槽。

    更新:

    这是一个使用两个按钮制作的迷你实现:

    <强> MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class QPushButton;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        QPushButton *mAmputationButton;
        QPushButton *mAsthmaButton;
    
    private slots:
        void on_pushButton_clicked();
    
    };
    

    <强> MainWindow.cpp

    #include "MainWindow.h"
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QDialog>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        mAmputationButton( new QPushButton("Amputation" ) ),
        mAsthmaButton( new QPushButton("Astham" ) )
    {
        setWindowTitle("First Aid");
    
        QWidget *sampleWidget = new QWidget();
        QVBoxLayout *lay = new QVBoxLayout();
        lay->addWidget(mAmputationButton);
        connect(mAmputationButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked );
        connect(mAsthmaButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked );
        lay->addWidget(mAsthmaButton);
        sampleWidget->setLayout(lay);
        setCentralWidget(sampleWidget);
    }
    
    MainWindow::~MainWindow(){}
    
    void MainWindow::on_pushButton_clicked(){
        QDialog *sampleDialog = new QDialog();
        sampleDialog->setModal(true);
        sampleDialog->show();
    }
    

    注1:在广告位on_pushbutton_clicked中,我只是创建一个新对话框并显示它。只需在那里添加插槽逻辑,就可以了。

    注2:建议您使用上述Initialize Connections之类的单一方法建立所有连接。

    将这个迷你实现作为一个例子,您可以在其上工作而不是复制粘贴使用它。