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?
Qt 6.11 is out! See what's new in the release blog

Does splitting widgets into multiple ui forms reduces compilation time?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 1.1k 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 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