简体   繁体   English

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

[英]Error while connecting QPushButton::clicked signal to a slot

First of all, I created about 20 push buttons using for loop.首先,我使用 for 循环创建了大约 20 个按钮。 And named them using if else loop.并使用 if else 循环命名它们。 Now, I want to connect each push buttons with the new dialog box.现在,我想将每个按钮与新对话框连接起来。 If I had used the design mode of QT, it would show me the name of the button when I press connect(ui->pushButton_0, SIGNAl(released()), SLOT(digit_pressed()) something like this. But, I don't know the name of the pushbutton I made as the for and if else loop made it. The connect(ui-> .......) also doesn't show any predictions. How can I link these push buttons and a new dialog box?如果我使用 QT 的设计模式,当我按下connect(ui->pushButton_0, SIGNAl(released()), SLOT(digit_pressed())类似这样的东西时,它会显示按钮的名称。但是,我不不知道我作为 for 和 if else 循环制作的按钮的名称connect(ui-> .......)也没有显示任何预测。我如何链接这些按钮和一个新的对话框?

Here is my code:这是我的代码:

mainwindow.h主窗口.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主窗口.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();
}

**In these codes, I have tried my luck by creating on_pushButton_clicked() function. **在这些代码中,我通过创建on_pushButton_clicked()函数来on_pushButton_clicked()运气。 But, it was just a try.但是,这只是尝试。 ** **

As in programming everything is cooking, :), let's see what are the ingredients of connect() :就像在编程中一切都在做饭一样,:),让我们看看connect()的成分是什么:

connect(sender, &Sender::signal, receiver, &Receiver::slot);

so sender would be the buttons , the signal is the clicked , the receiver itself, that is, this , and the slot on_pushButton_clicked所以发送者是buttons ,信号是clicked ,接收者本身,即this ,以及插槽on_pushButton_clicked

I see unnecessary if-else, everything can be reduced to a for:我看到不必要的 if-else,一切都可以简化为:

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


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

    QVBoxLayout *lay = new QVBoxLayout();
    QStringList names{"Amputation", "Asthama", "Bleeding", "Burns", "Chest Pain",
                      "Drowning", "Diarrhoea", "Epilepsy", "Fainting", "Fever",
                      "Food Poisoning", "Fracture", "Head Injury", "Muscle Strain",
                     "No breathing", "Nose bleed", "Poisoning", "Snake Bites",
                      "Stroke","Sun Burn", "Testicle Pain", "Ulcer", "Child delievery",
                      "Heart Attack", "Gastric"};
    for(const QString & name: names)
    {
        QPushButton *button = new QPushButton(name);
        lay->addWidget(button);
        connect(button, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked)

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

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

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

Note:笔记:

avoid using the old style of connection, has many disadvantages, read the content of the following link for more information:避免使用旧的连接方式,有很多缺点,请阅读以下链接的内容以获取更多信息:

  1. Make all these QPushButton members of a class such that they are created and destroyed properly rather than creating them in an loop.使所有这些QPushButton成员成为一个类,以便正确创建和销毁它们,而不是在循环中创建它们。

  2. Once you have each one as a separate member, then have a method InitializeConnections() and make all the connections under it using the Qt connect syntax.一旦你把每一个都作为一个单独的成员,然后有一个方法InitializeConnections()并使用 Qt 连接语法建立它下的所有连接。

If you believe these buttons are part of the MainWindow class, then your class can look something like this :如果您认为这些按钮是MainWindow类的一部分,那么您的类可能如下所示:

class MainWindow{
    ...
    ...
    private :
      // Will make connections of each button to it's respective slot.
      void InitializeConnections();

    private :
      QPushButton *mAmputationButton;
      QPushButton *mAsthmaButton;
    //.. so on
};

And in MainWindow.cpp :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.
}

The slots that i have mentioned are just for example, connect it to the slot that you need it to be connected to.我提到的插槽只是例如,将其连接到您需要连接的插槽。

UPDATE :更新 :

Here is a mini-implementation made using just the two buttons :这是仅使用两个按钮进行的​​小型实现:

MainWindow.h主窗口.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();
}

Note1 : In the slot on_pushbutton_clicked , I am just creating a new dialog and showing it.注意1:在槽on_pushbutton_clicked ,我只是创建一个新对话框并显示它。 Just add your slot logic there and you will be good to go.只需在那里添加您的插槽逻辑,您就可以开始了。

Note2 : It will be advisable to have all your connections in a single method such as Initialize Connections as already mentioned above.注意2:建议将所有连接都放在一个方法中,例如上面已经提到的Initialize Connections

Take this mini-implementation just as an example that you can work on top of rather than a copy-paste use of it.以这个小型实现为例,您可以在它之上工作,而不是复制粘贴使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM