How to convert .ui to .h?
-
@StudentScripter You can and must do it without editing the generated header file.
-
@Christian-Ehrlicher @sierdzio but wasn't that possible in the older versions using uic?
https://stackoverflow.com/questions/59202941/how-to-convert-ui-fully-into-c-header-and-source-file -
@StudentScripter
Wasn't what possible? (The link you give says nothing new, the guy just needed to runuic
correctly.) And it has always been the same.uic
reads your.ui
and produces aui_....h
file, which you then include. You do not edit theui_....h
file in any way, since if you did it would get overwritten the next time you change the.ui
/runuic
. Instead you write your own code in the.cpp
file which includes theui_....h
, like everyone else does. I don't see any problem or why you seem to want to complicate this. -
@JonB @sierdzio Yeah thanks, well maybe i explained it very badly but thats exactly what i want to do. I want to edit the cpp file. But i can't get the uic to give me the .h and .cpp file.
I made an extra testproject with the dialog.ui :
Now i have a ui build inside the dialog.ui:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Dialog</class> <widget class="QDialog" name="Dialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Dialog</string> </property> <widget class="QWidget" name=""> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>401</width> <height>311</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>PushButton</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pushButton_2"> <property name="text"> <string>PushButton</string> </property> </widget> </item> </layout> </widget> </widget> <resources/> <connections/> </ui>
How can i make it so these things get converted and appear in the .cpp?
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; }
-
@StudentScripter Your UI elements are accessible in Dialog class under
ui
object.For example:
ui->pushButton_2->setText("Hello!");
-
@sierdzio i know this method but is there a way to convert the form completely to code after creating it so i don't have to go over ui-> all the time. Also i want to see the layout structure i created and so on in my code. Is there a way with form?
-
@StudentScripter said in How to convert .ui to .h?:
. Also i want to see the layout structure i created and so on in my code. Is there a way with form?
You see them, in the generated header. What else is needed? If you want to do everything by yourself then simply don't use ui files.
-
@Christian-Ehrlicher I don't see them in the generated headder. I can access the form by ui->pushbutton usw.
but i don't see it in the headder.
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = nullptr); ~Dialog(); private: Ui::Dialog *ui; }; #endif // DIALOG_H
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; }
Sorry if we're talking past each other.
-
@StudentScripter said in How to convert .ui to .h?:
Also i want to see the layout structure i created and so on in my code. Is there a way with form?
As already mentioned by the others, it makes no sense.
You seem to misunderstand the purpose of the whole QtDesigner/Form-Editor/UI-File stuff...
It is used to not write too much GUI related code or at least simplify the GUI creation/design process.
If you decide to use any*.ui
file, you have to deal with the fact that "product" whichuic
outputs is somewhat static.
Of course you can add or remove stuff by using the pointer to the generated Form class in your actual code. This is recommended and nothing special, but if you do it in the generated header file directy and try to "inject" changes there, every time you re-compile your program (assuming you also re-runuic
) everything in your "Form" header will be overridden since it solely generates the header from the XML style definitions in*.ui
. -
@StudentScripter
You do see everything from your.ui
being processed byuic
. It is theui_dialog.h
you have included which contains that. That is alluic
generates (and you will find it in your build output directory, not your source directory), no.cpp
file. And it makes it so all the widgets you designed live in theUi
namespace. You should welcome that and stick with it, not attempt to change it.And btw: what that means is you will have two classes named
Dialog
: one is the plainDialog
you are creating in your own.cpp
&.h
files, where you can write whatever code you like. The generatedui_....h
file also contains a class nameDialog
but it is in theUi
namespace. And the class memberui
variable is an instance of that. Whose members (your widgets) are accessed viaui->...
, so they do not interfere with anything you write in yourDialog
class.Have a look at the generated
ui_dialog.h
file and everything should become clear. Note that I have told you above which directory that is in. -
@StudentScripter said in How to convert .ui to .h?:
but i don't see it in the headder.
Because you are looking at the wrong header...
If you want to see yourui->pushbutton
in "code", open theui_dialog.h
and have a look, but you should not modify it there.Edit:
@JonB just modified his post and said what you need to know about theui
pointer and the header...I still don't understand why you think you need to do something like this... What's wrong with using the
ui->
pointer to access your generated GUI class? Or just do everything in code and don't use any*.ui
file at all. -
@Pl45m4 Well my idea was: sometimes it's easier to just prototype a quick layout using the form, but for project organisation and total overview of whats going on i like to have everything in code, so when reading the code i directly see which and how many layouts where used.
But before i forget thanks you all for your input. I guess i just stick with writing everything in code in this case.
-
Then simply copy the stuff from setupUi() over to your ctor.
-
@StudentScripter
Like I said, don't forget it's perfectly possible to look inside theui_....h
file to understand/work with it, just like your own code. You can open it inside Creator, and I imagine (can't remember the key press, maybe F2 does it?) directly from the#include "ui_dialog.h"
line. So you really do have the source code there while you're developing. Just don't change anything that is in that file! -
-
@StudentScripter Yes, you can do that. And it shows you how they implemented stuff.