将按钮连接到主窗口插槽

时间:2013-12-10 16:58:51

标签: qt slots

我试图对如何解决这个问题做一堆研究,一切都与我的情况略有不同,或者没有解决我的问题。我将首先解释我的主要目标。我有一个主窗口,上面有7个按钮(除此之外),当你按下每个按钮时,它会关闭当前窗口并打开一个新窗口。所有窗口都有相同的7个按钮,因此您可以在每个窗口之间切换。由于所有窗口都具有完全相同的7个按钮,我想设置一个函数,每个类可以调用它来设置每个按钮并连接到mainwindow.cpp中的一个槽()(在下面的示例中称为setupSubsystemButtons)。实际按钮被放置在那里,但它们只能在我从mainwindow.cpp中按下时才能工作....当我从另一个班级按下它们时没有任何事情发生。

mainwindow.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

mainwindow.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);
};

1 个答案:

答案 0 :(得分:1)

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

这将创建按钮并将其信号连接到setupButtons的插槽,一旦超出范围(ECSgeneralCommand构造函数的结尾),它们就会被删除。所以你的按钮将保持连接状态。

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

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

相关问题