Qt window wont close using "this->close()" from other class

1.6k views Asked by At

I will start off by explaining my main goal. I have a main window with 7 buttons on it(amongst other things), when you hit each button, it closes out the current window and opens up a new window. All the windows will have the same 7 buttons, so you can go between each window. With all windows having the exact same 7 buttons, I wanted to set up a function that each class can call to set up each button and connect to a slot() in my mainwindow.cpp(called setupSubsystemButtons in example below). However, I can't seem to get the window to close using the standard "this->close()"...it works when I go from the main window to another window(the main window closes) but when I go from a different window to say the home window, the different window doesn't close. Suggestions would be greatly appreciated. My guess is that my understanding of "this" when it comes to calling slots in another class is wrong.

mainwindow.cpp( the parts that are relevant)

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 ECS 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()));
}

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 slots:

public slots:
    void ECSgeneralScreen();
    void homeScreen();

};

#endif // MAINWINDOW_H

ecsgeneralcommandWindow

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(650);

    ...


    //Setup Subsystem Buttons
    test.setupSubsystemButtons(layout);

    setLayout(layout);
}

ecsgeneralcommandWindow header

#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:
    MainWindow test;
public slots:

};

#endif // ECSGENERALCOMMAND_H
1

There are 1 answers

1
Pavel Strakhov On BEST ANSWER

Slots are just normal functions. When Qt invokes a slot, it ends up calling the appropriate receiver's method. In other words, this equals to the value of the 3rd argument of your connect statements. You passed this there, so the receiver is MainWindow object. E.g. MainWindow::homeScreen method always tries to close MainWindow. If it is already hidden, this action takes no effect.

You should either have a slot in each window class and connect buttons to appropriate receivers, or use a pointer to the currently active window instead of this when calling close(). But your architecture is strange in the first place. Why would you need to create these buttons for each window? It is reasonable to create them once and use in all windows. Also hiding and showing windows is not necessary. You can create one main window with buttons and a QStackedWidget that will contain the content of all other windows. Maybe you can even use QTabWidget instead of these buttons.