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. Problem with switching UI files. (Solved)
Forum Updated to NodeBB v4.3 + New Features

Problem with switching UI files. (Solved)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.2k Views 1 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.
  • S Offline
    S Offline
    SheerFreeze4
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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);
      @

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SheerFreeze4
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

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

          (Z(:^

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SheerFreeze4
            wrote on last edited by
            #5

            Thanks so much that did it!

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Pleasure. Please mark the thread as solved.

              (Z(:^

              1 Reply Last reply
              0

              • Login

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