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. Does splitting widgets into multiple ui forms reduces compilation time?
Forum Updated to NodeBB v4.3 + New Features

Does splitting widgets into multiple ui forms reduces compilation time?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 1.0k 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.
  • D Offline
    D Offline
    Daniella
    wrote on last edited by
    #1

    Does splitting widgets into multiple .ui files reduce compilation time? I'm compiling on Visual Studio 2022.

    Obviously, I would not add one widget per .ui but complex widgets with a lot of children.

    Also, I'm 'including' the new .ui correctly in the code below?

    ui_widget.h is a widget.ui created with the Visual Studio option Qt Widgets Form File

    // mainwindow.h
    #include <QtWidgets/QMainWindow>
    #include "ui_MainWindow.h"
    #include "ui_widget.h"
    
    
    class MainWindow: public QMainWindow, public Ui::MainWindowClass
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        Ui::MainWindowClass ui;
        Ui::Form form;
         
    };
    
    // mainwindow.cpp
    #include "stdafx.h"
    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        ui.setupUi(this);
        form.setupUi(this); // <- is this correct?
    
        ui.stackedWidget->insertWidget(0, form.pushButton);
        form.pushButton->show();
    }
    

    I asked this same question on StackOverFlow and on comment people suggest something to avoid including the form
    using namespace, but i don't understand what they mean.

    JonBJ 1 Reply Last reply
    1
    • D Daniella

      Does splitting widgets into multiple .ui files reduce compilation time? I'm compiling on Visual Studio 2022.

      Obviously, I would not add one widget per .ui but complex widgets with a lot of children.

      Also, I'm 'including' the new .ui correctly in the code below?

      ui_widget.h is a widget.ui created with the Visual Studio option Qt Widgets Form File

      // mainwindow.h
      #include <QtWidgets/QMainWindow>
      #include "ui_MainWindow.h"
      #include "ui_widget.h"
      
      
      class MainWindow: public QMainWindow, public Ui::MainWindowClass
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
          Ui::MainWindowClass ui;
          Ui::Form form;
           
      };
      
      // mainwindow.cpp
      #include "stdafx.h"
      #include "mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
      {
          ui.setupUi(this);
          form.setupUi(this); // <- is this correct?
      
          ui.stackedWidget->insertWidget(0, form.pushButton);
          form.pushButton->show();
      }
      

      I asked this same question on StackOverFlow and on comment people suggest something to avoid including the form
      using namespace, but i don't understand what they mean.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Daniella
      Each .ui file produces (via uic) one ui_....h file. And that #includes the Qt headers. So If anything my guess would be that the fewer .ui files you have the faster the compilation, each little extra one may add some overhead!

      Not that I would want to take this into account when developing, one monolithic .ui file to save others/cut down on compilation seems a bad approach for development!

      You might find in VS that using "precompiled headers" option wherever possible would be best. The ui_....h file is not regenerated unless you change the .ui file, so a lot of the time it would not need to be recompiled?

      D 1 Reply Last reply
      0
      • JonBJ JonB

        @Daniella
        Each .ui file produces (via uic) one ui_....h file. And that #includes the Qt headers. So If anything my guess would be that the fewer .ui files you have the faster the compilation, each little extra one may add some overhead!

        Not that I would want to take this into account when developing, one monolithic .ui file to save others/cut down on compilation seems a bad approach for development!

        You might find in VS that using "precompiled headers" option wherever possible would be best. The ui_....h file is not regenerated unless you change the .ui file, so a lot of the time it would not need to be recompiled?

        D Offline
        D Offline
        Daniella
        wrote on last edited by
        #3

        @JonB I see, do you know whats the namespace thing they suggest, to avoid including the .h file?

        jsulmJ 1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          The way of using ui file in Qt is from MVC pattern. ui file is the View and can be made by non-programmers(for example artists). If you design ui files by yourself, you may not need ui files at all. But for maintenance purpose, it is better to use ui files since ui might be changed often. I guess compiling time is not an issue here. Not sure how big your project is and ui files will affect compiling time.

          1 Reply Last reply
          0
          • D Daniella

            @JonB I see, do you know whats the namespace thing they suggest, to avoid including the .h file?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Daniella said in Does splitting widgets into multiple ui forms reduces compilation time?:

            do you know whats the namespace thing they suggest

            I guess they suggest to not to include the ui header file in your widget header file but in the widget cpp file. This is common practise in C++. It also uses forward declaration:

            class ui::YOUR_UI_CLASS; // This is forward declaration
            
            class YourWidget
            private:
                ui::YOUR_UI_CLASS *ui;
            

            It is always good to use forward declarations for types which are used as pointers. Because then you're including less header files in your header files.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Daniella said in Does splitting widgets into multiple ui forms reduces compilation time?:

              do you know whats the namespace thing they suggest

              I guess they suggest to not to include the ui header file in your widget header file but in the widget cpp file. This is common practise in C++. It also uses forward declaration:

              class ui::YOUR_UI_CLASS; // This is forward declaration
              
              class YourWidget
              private:
                  ui::YOUR_UI_CLASS *ui;
              

              It is always good to use forward declarations for types which are used as pointers. Because then you're including less header files in your header files.

              D Offline
              D Offline
              Daniella
              wrote on last edited by Daniella
              #6

              fbd1feb4-3fa0-4a28-8fc2-cf49c210a050-image.png

              What option i should select here? Member pointer?

              And then:

              // .h
              #pragma once
              #include <QtWidgets/QMainWindow>
              
              QT_BEGIN_NAMESPACE
              namespace Ui { class MainWindowClass; };
              QT_END_NAMESPACE
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              public:
                  MainWindow(QWidget *parent = nullptr);
                  ~MainWindow();
              
              private:
                  Ui::MainWindowClass *ui;
              };
              

              //.cpp
              #include "stdafx.h"
              #include "MainWindow.h"
              #include "ui_MainWindow.h"
              
              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindowClass())
              {
                  ui->setupUi(this);
              }
              

              This way including ui_MainWindow.h into the .cpp?

              jsulmJ 1 Reply Last reply
              0
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • D Daniella

                  fbd1feb4-3fa0-4a28-8fc2-cf49c210a050-image.png

                  What option i should select here? Member pointer?

                  And then:

                  // .h
                  #pragma once
                  #include <QtWidgets/QMainWindow>
                  
                  QT_BEGIN_NAMESPACE
                  namespace Ui { class MainWindowClass; };
                  QT_END_NAMESPACE
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  public:
                      MainWindow(QWidget *parent = nullptr);
                      ~MainWindow();
                  
                  private:
                      Ui::MainWindowClass *ui;
                  };
                  

                  //.cpp
                  #include "stdafx.h"
                  #include "MainWindow.h"
                  #include "ui_MainWindow.h"
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindowClass())
                  {
                      ui->setupUi(this);
                  }
                  

                  This way including ui_MainWindow.h into the .cpp?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Daniella said in Does splitting widgets into multiple ui forms reduces compilation time?:

                  it didnt compile

                  Then please post the compiler error.
                  The code you posted should work.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  D 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Daniella said in Does splitting widgets into multiple ui forms reduces compilation time?:

                    it didnt compile

                    Then please post the compiler error.
                    The code you posted should work.

                    D Offline
                    D Offline
                    Daniella
                    wrote on last edited by
                    #9

                    @jsulm I edited my previous answer, is it 'correct' now? whats the advantage of this 'method'?

                    jsulmJ 1 Reply Last reply
                    0
                    • D Daniella

                      @jsulm I edited my previous answer, is it 'correct' now? whats the advantage of this 'method'?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Daniella said in Does splitting widgets into multiple ui forms reduces compilation time?:

                      whats the advantage of this 'method'?

                      I already explained what the advantage is. In short: it reduces compile times. You can also google for "C++ forward declaration".

                      "What option i should select here?" - member pointer. Forward declarations only work with pointers. For non-pointer members the compiler needs to know the type exactly already in the header file.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        Daniella
                        wrote on last edited by Daniella
                        #11

                        Thank you, i understand now.
                        Is possible to modify an existing visual studio project to generate the .ui as member pointer?

                        jsulmJ 1 Reply Last reply
                        0
                        • D Daniella

                          Thank you, i understand now.
                          Is possible to modify an existing visual studio project to generate the .ui as member pointer?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Daniella Yes, just edit your code accordingly.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          D 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Daniella Yes, just edit your code accordingly.

                            D Offline
                            D Offline
                            Daniella
                            wrote on last edited by
                            #13

                            @jsulm I'm trying to edit it.

                            Question, how do i access the ui from others .cpp files than the mainwindow.cpp?

                            When i try #include mainwindow.h and try ui->centralWidget i get this error:

                            "pointer to incomplete class type "Ui::MainWindow" is not allowed
                            
                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • D Daniella

                              @jsulm I'm trying to edit it.

                              Question, how do i access the ui from others .cpp files than the mainwindow.cpp?

                              When i try #include mainwindow.h and try ui->centralWidget i get this error:

                              "pointer to incomplete class type "Ui::MainWindow" is not allowed
                              
                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Daniella said in Does splitting widgets into multiple ui forms reduces compilation time?:

                              Question, how do i access the ui from others .cpp files than the mainwindow.cpp?

                              You must not - write proper functions to access the ui elements.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              D 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                @Daniella said in Does splitting widgets into multiple ui forms reduces compilation time?:

                                Question, how do i access the ui from others .cpp files than the mainwindow.cpp?

                                You must not - write proper functions to access the ui elements.

                                D Offline
                                D Offline
                                Daniella
                                wrote on last edited by Daniella
                                #15

                                @Christian-Ehrlicher said in Does splitting widgets into multiple ui forms reduces compilation time?:

                                write proper functions to access the ui elements.

                                Could you give an example?

                                // main.cpp
                                #include "mainwindow.h"
                                
                                int main(int argc, char *argv[])
                                {
                                    QApplication a(argc, argv);    
                                    MainWindow w;   
                                    w.show();
                                }
                                
                                // mainwindow.h
                                #include <QtWidgets/QMainWindow>
                                
                                QT_BEGIN_NAMESPACE
                                namespace Ui { class MainWindowClass; class Form; };
                                QT_END_NAMESPACE
                                
                                class MainWindow : public QMainWindow
                                {
                                    Q_OBJECT
                                public:
                                    MainWindow(QWidget *parent = nullptr);
                                    ~MainWindow();
                                    void Test();
                                    Ui::MainWindowClass *ui;
                                    Ui::Form *form;
                                };
                                
                                // mainwindow.cpp
                                #include "stdafx.h"
                                #include "MainWindow.h"
                                #include "ui_MainWindow.h"
                                #include "ui_Form.h"
                                
                                MainWindow::MainWindow(QWidget *parent)
                                    : QMainWindow(parent)
                                    , ui(new Ui::MainWindowClass())
                                {
                                    ui->setupUi(this);
                                    form->setupUi(this);
                                }
                                
                                // test.cpp
                                #include "mainwindow.h"
                                
                                void MainWindow::Test()
                                { 
                                     ui->centralWidget; // <-- error: "pointer to incomplete class type "Ui::MainWindowClass" is not allowed
                                }
                                

                                how do i access ui in Test()?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Hi,

                                  You are missing

                                  #include "ui_MainWindow.h"
                                  

                                  in your test.cpp file.

                                  Out of curiosity, why are you creating a new file for that method ? You are making your codebase more complex for no benefit.

                                  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
                                  1

                                  • Login

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