Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStackedWidget with multiple classes
Forum Updated to NodeBB v4.3 + New Features

QStackedWidget with multiple classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstackedwidgetqstackedwidgets
2 Posts 2 Posters 601 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by hobbyProgrammer
    #1

    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");
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved