Problem with switching UI files. (Solved)



  • Hello I've spent the last month learning Qt. I understand it fairly well yet I have a problem. I used Qt Creator to create different .ui files and then set up the program to switch between them. Here is my problem, when I run my program it creates two windows, the first and second, but the second one is just a frame of the window. When I press the push button it hides the first window and the second one (the frame) shows it's content. I need the second window to show up when I press the button and I need to know how to get the frame to vanish until then. My code is here.

    main.cpp
    @#include "mainwindow.h"
    #include "mainwindow.cpp"
    #include <QApplication>
    using namespace std;

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow qw;
    qw.show();
    return a.exec();
    }
    @

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "secwindow.h"
    #include "ui_secwindow.h"
    #include <iostream>
    int Mode;

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

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    void MainWindow::on_AdminRadio_toggled(bool checked)
    {
    Mode= (checked==true) ? 1:0;

    Mode= (checked==false) ? 0:1;
    

    }

    void MainWindow::on_UserRadio_toggled(bool checked)
    {
    Mode= (checked==true) ? 2:0;

    Mode= (checked==false) ? 0:2;
    

    }

    void MainWindow::on_NextButton_pressed()
    {
    if (Mode!=0){
    hide();
    sw.show();}

    }

    @

    secwindow.cpp
    @#include "secwindow.h"
    #include "ui_secwindow.h"

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

    SecWindow::~SecWindow()
    {
    delete ui;
    }
    @

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "secwindow.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private slots:

    void on_AdminRadio_toggled(bool checked);
    
    void on_UserRadio_toggled(bool checked);
    
    void on_NextButton_pressed();
    

    private:
    Ui::MainWindow *ui;
    SecWindow sw;
    };

    #endif // MAINWINDOW_H
    @

    secwindow.h
    @#ifndef SECWINDOW_H
    #define SECWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class SecWindow;
    }

    class SecWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::SecWindow *ui;
    };

    #endif // SECWINDOW_H
    @
    Thanks,
    Brooks Rady


  • Moderators

    Your second window needs to be reparented properly. When a QWidget is created without a parent (you are doing exactly that), it will create a full window frame.
    Possible solution (there are other ways, too):
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    sw.setParent(this);
    }
    @

    Or, more conventionally, declare SecWindow as a pointer and use this in constructor:
    @
    // MainWindow constructor
    sw = new SecWindow(this);
    @



  • Hi, I've trying out both of those methods and have been tinkering with them for quite a while now; however I haven't had much luck. It took me awhile to get it to compile, then when I put the code somewhere were it did, the two windows overlapped each other. Please, if you can post the exact code I need to put in because I don't have much longer before this needs to be done.

    Thanks,
    Brooks Rady


  • Moderators

    You might want to investigate QWizard for your use case, it provides kind of that functionality. Or QUILoader.

    Here is something that could work:
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include "secwindow.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_AdminRadio_toggled(bool checked);
    void on_UserRadio_toggled(bool checked);
    void on_NextButton_pressed();

    private:
    Ui::MainWindow *ui;
    SecWindow *sw;
    };

    // MainWindow.cpp
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    sw = NULL;
    }
    // ...
    void MainWindow::on_NextButton_pressed()
    {
    if (!sw) {
    sw = new SecWindow(this);
    sw->show();
    hide();
    }
    }
    @



  • Thanks so much that did it!


  • Moderators

    Pleasure. Please mark the thread as solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.