将QPush按钮::单击信号到插槽时出错
Error while connecting QPushButton::clicked signal to a slot
首先,我使用 for 循环创建了大约 20 个按钮。并使用 if else 循环命名它们。现在,我想将每个按钮与新对话框连接起来。如果我使用了QT的设计模式,当我按下类似的东西时,它会显示connect(ui->pushButton_0, SIGNAl(released()), SLOT(digit_pressed())
按钮的名称。但是,我不知道我作为 for 制作的按钮的名称,如果其他循环制作了它。connect(ui-> .......)
也没有显示任何预测。如何链接这些按钮和新对话框?
这是我的代码:
主窗口.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
主窗口.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()
函数来尝试运气。但是,这只是一次尝试。**
就像在编程中一样,一切都在烹饪,:),让我们看看connect()
的成分是什么:
connect(sender, &Sender::signal, receiver, &Receiver::slot);
所以发送方将是buttons
,信号是clicked
,接收方本身,即this
,插槽on_pushButton_clicked
我看到不必要的如果-否则,一切都可以简化为:
#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();
}
注意:
避免使用旧式连接,有很多缺点,请阅读以下链接的内容以获取更多信息:
- https://wiki.qt.io/New_Signal_Slot_Syntax
-
使所有这些
QPushButton
成员都成为类的成员,以便正确创建和销毁它们,而不是在循环中创建它们。 -
一旦你把每个作为一个单独的成员,那么有一个方法
InitializeConnections()
并使用Qt connect语法建立它下的所有连接。
如果您认为这些按钮是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.
}
我提到的插槽只是例如,将其连接到您需要连接的插槽。
更新:
这是一个仅使用两个按钮的迷你实现:
主窗口.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();
};
主窗口.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
。
以这个小型实现为例,您可以在其上运行,而不是复制粘贴使用它。
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 从Q_INVOKABLE功能或插槽中更改QQuick图像源
- 升压信号2将插槽传递到成员功能以断开连接
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- C++ 信号和插槽不工作:插槽不响应事件
- NS3 插槽混淆(需要帮助理解)
- QObject::连接无法将信号连接到*this*对象的插槽
- QT 插槽未在主线程上调用
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 控制带有信号/插槽的Qt QML滑动视图
- Qt 信号/插槽问题
- QT5 信号不会激活插槽内的功能
- Qt5:连接:如果插槽的参数少于信号,如何使用"连接"
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- 将QPush按钮::单击信号到插槽时出错
- 添加按钮时出错,即使存在这样的插槽,也不存在
- 将工作线程信号与主窗口插槽 (Qt5) 连接时出错
- 没有插槽,但 QMetaObject::connectSlotsByName 使用 Qt 和 C++ 时出错
- 升压信号2 - 通过插槽断开连接时出错