gpt4 book ai didi

c++ - QPushButton在单击时未执行连接功能

转载 作者:行者123 更新时间:2023-12-02 10:04:59 29 4
gpt4 key购买 nike

我有一个叫做UserInterface的类。在此类中,有不同的功能。 build_start_screen()为初始启动屏幕添加所有小部件(标签,按钮等)。 build_option_a_screen()从用户启动屏幕中删除所有内容,并在用户单击选项A的按钮时添加屏幕所需的所有小部件,依此类推。为了这个问题,该类被剥夺了。

现在,我在build_start_screen()中声明了一个按钮,并将其连接到简单的MessageBox.exec(),因此单击时应弹出该按钮。
但是,单击按钮后什么也没有发生。

我究竟做错了什么?与函数完成后变量的生存期有关吗?

#include <QApplication>
#include <QPushButton>
#include <QAbstractButton>
#include <QLabel>
#include <QFont>
#include <QVBoxLayout>
#include <QMessageBox>

//Class handling all the UI in this Application
class UserInterface {
public:
//Build the initial UI the user sees
void build_start_screen(QWidget& window) {
//Make new QVBoxLayout for this startscreen UI
this->layout = new QVBoxLayout(&window);

//Test messagebox
QMessageBox msgBox;
msgBox.setText("Button test.");

//Button to go to Option A-screen
QPushButton* showMsgBox = new QPushButton("Show pop-up");
QAbstractButton::connect(showMsgBox, SIGNAL (clicked()), &window, SLOT (msgBox.exec()));

//Add labels and button to QVBoxLayout
layout->addWidget(showMsgBox);
}

private:
//Properties
QVBoxLayout* layout;
};

int main(int argc, char **argv) {
QApplication app (argc, argv);

//Initialize Window
QWidget Window;
Window.resize(400, 250);

//Create new UserInterface object
//This will allow us to create different user-interfaces
//depending on the function we call
UserInterface* ui = new UserInterface();
ui->build_start_screen(Window);
Window.show();

return app.exec();
}


如果我想做同样的事情,但是我不想调用messageBox而是调用另一个函数怎么办?
#include <QApplication>
#include <QPushButton>
#include <QAbstractButton>
#include <QLabel>
#include <QFont>
#include <QVBoxLayout>
#include <QMessageBox>

//Class handling all the UI in this Application
class UserInterface {
public:
//Build the initial UI the user sees
void build_start_screen(QWidget& window) {
//Make new QVBoxLayout for this startscreen UI
this->layout = new QVBoxLayout(&window);

//Test messagebox
QMessageBox msgBox;
msgBox.setText("Button test.");

//Button to go to Option A-screen
QPushButton* showMsgBox = new QPushButton("Show pop-up");
QAbstractButton::connect(showMsgBox, SIGNAL (clicked()), &window, SLOT (build_option_a_screen()));

//Add labels and button to QVBoxLayout
layout->addWidget(showMsgBox);
}

void build_option_a_screen(QWidget& window) {
//Do stuff here with window
//e.g
window.resize(500, 500);
}

private:
//Properties
QVBoxLayout* layout;
};

int main(int argc, char **argv) {
QApplication app (argc, argv);

//Initialize Window
QWidget Window;
Window.resize(400, 250);

//Create new UserInterface object
//This will allow us to create different user-interfaces
//depending on the function we call
UserInterface* ui = new UserInterface();
ui->build_start_screen(Window);
Window.show();

return app.exec();
}

最佳答案

您的代码有2个问题:

  • 窗口“对象”没有错误所指出的插槽“msgBox.exec()”:
    QObject::connect: No such slot QWidget::msgBox.exec() in ../main.cpp:23
  • 更正以上内容,解决方法是:
    QObject::connect(showMsgBox, &QPushButton::clicked, &msgBox, &QMessageBox::exec);

    但是现在的问题是“msgBox”是一个本地变量,将被破坏并且无法显示。

  • 因此解决方案是使msgBox成为类的成员或指针(对于指针,您必须管理动态内存以避免内存泄漏):

    //Class handling all the UI in this Application
    class UserInterface {
    public:
    //Build the initial UI the user sees
    void build_start_screen(QWidget& window) {
    //Make new QVBoxLayout for this startscreen UI
    this->layout = new QVBoxLayout(&window);
    msgBox.setText("Button test.");
    //Button to go to Option A-screen
    QPushButton* showMsgBox = new QPushButton("Show pop-up");
    QObject::connect(showMsgBox, &QPushButton::clicked, &msgBox, &QMessageBox::exec);

    //Add labels and button to QVBoxLayout
    layout->addWidget(showMsgBox);
    }

    private:
    //Properties
    QVBoxLayout* layout;
    QMessageBox msgBox;
    };

    加号:

    建议不要使用旧的连接语法,因为它有局限性并隐藏了问题。

    建议不要使用旧的连接语法,因为它有局限性并隐藏了问题。

    如果要连接到不是QObject的某种方法(例如,要使用OP则为X),则解决方案是使用lambda方法:

    //Class handling all the UI in this Application
    class UserInterface {
    public:
    //Build the initial UI the user sees
    void build_start_screen(QWidget& window) {
    //Make new QVBoxLayout for this startscreen UI
    this->layout = new QVBoxLayout(&window);
    //Button to go to Option A-screen
    QPushButton* showMsgBox = new QPushButton("Show pop-up");
    QObject::connect(showMsgBox, &QPushButton::clicked, [this, &window](){
    build_option_a_screen(window);
    });
    //Add labels and button to QVBoxLayout
    layout->addWidget(showMsgBox);
    }
    void build_option_a_screen(QWidget& window) {
    //Do stuff here with window
    //e.g
    window.resize(500, 500);
    }
    private:
    //Properties
    QVBoxLayout* layout;
    };

    关于c++ - QPushButton在单击时未执行连接功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60645890/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com