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

QStackedWidget with multiple classes



  • Hi,

    I just created a small app with two windows. I wanted to create 1 window with a stackedWidget that contains both ui files. I would like to edit the .h and .cpp files as minimal as possible, but like to separate both ui's in different classes.
    I have 1 screen with a button. If that button is pressed, the second ui will show. The second screen is a combobox and a button.
    Currently I have them combined and the build was succesfull, but the items in the second screen aren't added to the combobox. I think it's because of this line in the secondwindow.cpp:

    SecWindow::SecWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    

    As that might create a new Ui, but I would like to re-use the Ui from the MainWindow.
    Any other suggestions on how to keep these functions in two different classes are always welcome.

    This is my code:

    main.cpp:

    #include "mainwindow.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "secwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->stackedWidget->setCurrentIndex(0);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        ui->stackedWidget->setCurrentIndex(1);
        SecWindow *sw = new SecWindow(ui->stackedWidget->currentWidget());
        sw->print();
    }
    

    secwindow.h

    #ifndef SECWINDOW_H
    #define SECWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class SecWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit SecWindow(QWidget *parent = nullptr);
        ~SecWindow();
    
    private:
        Ui::MainWindow *ui;
        friend class MainWindow;
        void print();
    };
    
    #endif // SECWINDOW_H
    

    secwindow.cpp

    #include "secwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    
    SecWindow::SecWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    SecWindow::~SecWindow()
    {
        delete ui;
    }
    
    void SecWindow::print()
    {
        ui->comboBox->addItem("Item 1");
        ui->comboBox->addItem("Item 2");
    }
    

  • Lifetime Qt Champion

    Hi,

    You don't call show on your sw object and it's not part of your stacked widget.