Unsolved Does splitting widgets into multiple ui forms reduces compilation time?
-
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 awidget.ui
created with the Visual Studio optionQt 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. -
@Daniella
Each .ui
file produces (viauic
) oneui_....h
file. And that#include
s 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? -
@JonB I see, do you know whats the namespace thing they suggest, to avoid including the .h file?
-
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.
-
@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.
-
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
? -
This post is deleted! -
@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. -
@jsulm I edited my previous answer, is it 'correct' now? whats the advantage of this 'method'?
-
@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.
-
Thank you, i understand now.
Is possible to modify an existing visual studio project to generate the.ui
as member pointer? -
@Daniella Yes, just edit your code accordingly.
-
@jsulm I'm trying to edit it.
Question, how do i access the
ui
from others.cpp
files than themainwindow.cpp
?When i try
#include mainwindow.h
and tryui->centralWidget
i get this error:"pointer to incomplete class type "Ui::MainWindow" is not allowed
-
@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.
-
@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
inTest()
? -
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.