【问题标题】:Connecting buttons to mainwindows slot将按钮连接到主窗口插槽
【发布时间】:2013-12-10 16:58:51
【问题描述】:

我尝试对如何解决此问题进行大量研究,但一切都与我的情况略有不同,或者无法解决我的问题。我将首先解释我的主要目标。我有一个主窗口,上面有 7 个按钮(除其他外),当你点击每个按钮时,它会关闭当前窗口并打开一个新窗口。所有窗口都有相同的 7 个按钮,因此您可以在每个窗口之间切换。由于所有窗口都有完全相同的 7 个按钮,我想设置一个函数,每个类都可以调用该函数来设置每个按钮并连接到我的 mainwindow.cpp 中的 slot()(在下面的示例中称为 setupSubsystemButtons)。实际的按钮被放置在那里,但它们仅在从我的 mainwindow.cpp 按下时才起作用....当我从不同的类按下它们时,没有任何反应。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtWidgets>
#include <QDialog>


namespace Ui {
class MainWindow;
}

class MainWindow : public QDialog
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    QWidget *window;
    void setupSubsystemButtons(QGridLayout *layout);
    ~MainWindow();

private:
Ui::MainWindow *ui;

QLineEdit *tempValueBox;
QLineEdit *humidityValueBox;
QLineEdit *c02ValueBox;
...
public slots:
void ECSgeneralScreen();
void homeScreen();

};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ecsgeneralcommand.h"

#include <QtWidgets>
#include <QtCore>

MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
{

    QGridLayout *layout = new QGridLayout;
...
setLayout(layout);

}

void MainWindow::ECSgeneralScreen()
{
    ECSgeneralCommand *ECSgeneral = new ECSgeneralCommand;
    this->close();
    ECSgeneral->show();
    //opens up the ECS screen
}

void MainWindow::homeScreen()
{
    MainWindow *home = new MainWindow;
    this->close();
    home->show();
    //opens up the home screen
}

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

void MainWindow::setupSubsystemButtons(QGridLayout *layout)
{
    //Push Button Layout
    homeScreenButton = new QPushButton("Home");
    layout->addWidget(homeScreenButton, 3, 11);
    connect(homeScreenButton, SIGNAL(clicked()), this, SLOT(homeScreen()));


    ECSgeneralScreenButton = new QPushButton("General");
    layout->addWidget(ECSgeneralScreenButton,5,11);
    connect(ECSgeneralScreenButton, SIGNAL(clicked()), this, SLOT(ECSgeneralScreen()));

}

ecsgeneralcommand.h

#ifndef ECSGENERALCOMMAND_H
#define ECSGENERALCOMMAND_H

#include <QDialog>
#include <QMainWindow>
#include <QtWidgets>
#include <QObject>
#include "mainwindow.h"

class ECSgeneralCommand : public QDialog
{
    Q_OBJECT

public:
    explicit ECSgeneralCommand(MainWindow *parent = 0);

private:
    ...

public slots:

};

#endif // ECSGENERALCOMMAND_H

ecsgeneralcommand.cpp

#include "ecsgeneralcommand.h"
#include "mainwindow.h"

#include <QtWidgets>
#include <QtCore>

ECSgeneralCommand::ECSgeneralCommand(MainWindow *parent) :   QDialog(parent)
{
    QGridLayout *layout = new QGridLayout;
    QWidget::setFixedHeight(600);
    QWidget::setFixedWidth(550);

    ...

    MainWindow setupButtons;
    //Setup Subsystem Buttons
    setupButtons.setupSubsystemButtons(layout);


    setLayout(layout);
};

【问题讨论】:

    标签: qt slots


    【解决方案1】:
        MainWindow setupButtons;
        //Setup Subsystem Buttons
        setupButtons.setupSubsystemButtons(layout);
    

    这将创建按钮并将它们的信号连接到setupButtons 的插槽,一旦超出范围(ECSgeneralCommand 构造函数的末尾)就会被删除。因此,您的按钮将保持与任何内容的连接。

    您需要将按钮信号连接到按下按钮时将存在的对象,例如ECSgeneralCommand 本身。然后它可以自行关闭并生成正确的窗口。

    或者,如果适用于您的应用程序,可能是一个更好的解决方案:使用单个主窗口,带有QStackedWidget,当按下按钮时切换小部件。这就是通常的做法。

    【讨论】:

    • 我不知道 QStackedWidget,看起来这对我的应用程序来说是完美的。感谢您的回答和建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2015-06-09
    • 1970-01-01
    相关资源
    最近更新 更多