0

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();
}
Cesar
  • 41
  • 2
  • 5
  • 16
  • I doubt that it reduces compilation time. Including 1 file is likely faster than including many files. The general rule is 1 class - 1 ui file. Also, don't inherit from `Ui::*` if you use it as a class member. – Osyotr Jan 23 '23 at 18:33
  • @Osyotr i was told that multiple files compile faster because of incremental building, i dont understand about `dont inherit from Ui::*` – Cesar Jan 23 '23 at 18:41
  • Your code above is wrong - you either derive from Ui::Form or use a member with Ui::Form - not both. And if you use a pointer to Ui::Form as a member you can formward declare this class which prevents the need for the include in the header. – chehrlic Jan 23 '23 at 19:02
  • @Daniela in your case the number of files being compiled is one. – Osyotr Jan 23 '23 at 19:08
  • @chehrlic could you give an example? – Cesar Jan 23 '23 at 19:11
  • Example of what? Forward-declaring a class is c++ basics -> https://stackoverflow.com/questions/9119236/c-class-forward-declaration – chehrlic Jan 23 '23 at 21:15
  • @chehrlic I know what is class forward, I didn't understand what you said that could prevent the need for the include in the header. – Cesar Jan 23 '23 at 21:18
  • If you declare your `ui` and `form` members as pointers (=what Qt creator/VS Qt addin do), you can move the corresponding `#include`s from `.h` to `.cpp` files and declare the classes like so instead: `namespace Ui { class MainWindowClass; class Form; }`. This is likely to be the only way to speed up compilation. With your current approach, changing `MainWindow` means you indirectly change `mainwindow.h` too, which in turn means you need to recompile all the files that do `#include "mainwindow.h"`. With forward declaration, the only source you need to recompile is `mainwindow.cpp`. – Atmo Jan 24 '23 at 03:12
  • With all of the above said, the only benefit I see in splitting your ui files into `ui_MainWindow.h` and `ui_widget.h` is if you use either of them for more than 1 window. No benefit for compilation time. – Atmo Jan 24 '23 at 03:16
  • @Atmo: you're wrong - when you move the ui_foo.h into the source file you will gain compile speed because every other file which includes your header now no longer needs to include ui_foo.h – chehrlic Jan 24 '23 at 16:48
  • @chehrlic, is it not exactly what I said? Quote: "you can move the corresponding `#include`s from `.h` to `.cpp` files and declare the classes like so instead: `namespace Ui { class MainWindowClass; class Form; }`. **This is likely to be the only way to speed up compilation.**"What do you mean? – Atmo Jan 24 '23 at 19:05

0 Answers0