Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using a push button to reopen my main window.



  • I am creating a UI that has one main pages and two subpages. When going to the sub page I have the main page closing, I would like to then open the main page again on clicking a "back" button that I created. I keep getting an error that says modeselect does not name a type. Mode select is the page I am trying to return to. I am only trying to get the back button on the page called noheatmode to work right now. Below are the two .h files for the pages and the noheatmode.cpp

    modeselect.h file
    #ifndef MODESLECT_H
    #define MODESLECT_H

    #include <QMainWindow>
    #include "noheatmode.h"
    #include "heatmode.h"

    namespace Ui {
    class modeselect;
    }

    class modeselect : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit modeselect(QWidget *parent = 0);
    ~modeselect();

    private slots:
    void on_noHeatModeButton_clicked();

    void on_heatModeButton_clicked();
    

    private:
    Ui::modeselect *ui;
    noheatmode *noHeatMode;
    heatmode *heatMode;

    };

    #endif // MODESLECT_H

    noheatmode.h file

    #ifndef NOHEATMODE_H
    #define NOHEATMODE_H

    #include <QWidget>
    #include "modeselect.h"

    namespace Ui {
    class noheatmode;
    }

    class noheatmode : public QWidget
    {
    Q_OBJECT

    public:
    explicit noheatmode(QWidget *parent = 0);
    ~noheatmode();

    private slots:
    void on_noHeatModeBack_clicked();

    private:
    Ui::noheatmode *ui;
    modeselect *modeSelect;
    };

    #endif // NOHEATMODE_H

    noheatmode.cpp

    #include "modeselect.h"
    #include "noheatmode.h"
    #include "ui_noheatmode.h"

    noheatmode::noheatmode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::noheatmode)
    {
    ui->setupUi(this);
    }

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

    void noheatmode::on_noHeatModeBack_clicked()
    {
    modeSelect =new modeselect;
    modeSelect ->show();
    close();
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Don't you have circular dependencies issues ?

    In any case, your current design is wrong, you are trying to recreate each widget each time you go from one to the other and you are creating memory leaks.

    You should take a look at QStackedWidget/Layout in order to have a proper "widget switcher"



  • Thanks I will look into it. I was concerned that stacking pages on top of each other may add up to some issues, so i thought I would try this approach which apparently does not work.


  • Lifetime Qt Champion

    A QStackedWidget is really just that: you show one widget at a time and switch to the one you want/need when you want.


Log in to reply