0

我试图对如何解决这个问题进行大量研究,但一切都与我的情况略有不同,或者没有解决我的问题。我将首先解释我的主要目标。我有一个主窗口,上面有 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);
};
4

1 回答 1

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

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

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

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

于 2013-12-10T17:24:00.797 回答