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. How to convert .ui to .h?
Forum Update on Monday, May 27th 2025

How to convert .ui to .h?

Scheduled Pinned Locked Moved Solved General and Desktop
c++ qtconversion
19 Posts 5 Posters 2.4k Views
  • 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.
  • S StudentScripter
    12 Mar 2024, 11:45

    @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 : 7a229e60-f05f-40df-bae9-712712aa735e-image.png
    b5b97fdc-aa2b-4a2c-87e6-209e506e50fb-image.png

    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;
    }
    
    
    S Offline
    S Offline
    sierdzio
    Moderators
    wrote on 12 Mar 2024, 11:53 last edited by
    #8

    @StudentScripter Your UI elements are accessible in Dialog class under ui object.

    For example:

    ui->pushButton_2->setText("Hello!");
    

    (Z(:^

    S 1 Reply Last reply 12 Mar 2024, 12:25
    0
    • S sierdzio
      12 Mar 2024, 11:53

      @StudentScripter Your UI elements are accessible in Dialog class under ui object.

      For example:

      ui->pushButton_2->setText("Hello!");
      
      S Offline
      S Offline
      StudentScripter
      wrote on 12 Mar 2024, 12:25 last edited by
      #9

      @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?

      C P 2 Replies Last reply 12 Mar 2024, 12:32
      0
      • S StudentScripter
        12 Mar 2024, 12:25

        @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?

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 12 Mar 2024, 12:32 last edited by
        #10

        @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.

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

        S 1 Reply Last reply 12 Mar 2024, 13:13
        2
        • C Christian Ehrlicher
          12 Mar 2024, 12:32

          @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.

          S Offline
          S Offline
          StudentScripter
          wrote on 12 Mar 2024, 13:13 last edited by
          #11

          @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;
          }
          
          

          cd8a8662-7ad4-4e11-a6a6-015a6fada3d2-image.png

          Sorry if we're talking past each other.

          J P 2 Replies Last reply 12 Mar 2024, 13:19
          0
          • S StudentScripter
            12 Mar 2024, 12:25

            @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?

            P Offline
            P Offline
            Pl45m4
            wrote on 12 Mar 2024, 13:19 last edited by Pl45m4 3 Dec 2024, 13:21
            #12

            @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" which uic 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-run uic) everything in your "Form" header will be overridden since it solely generates the header from the XML style definitions in *.ui.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • S StudentScripter
              12 Mar 2024, 13:13

              @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;
              }
              
              

              cd8a8662-7ad4-4e11-a6a6-015a6fada3d2-image.png

              Sorry if we're talking past each other.

              J Offline
              J Offline
              JonB
              wrote on 12 Mar 2024, 13:19 last edited by JonB 3 Dec 2024, 13:25
              #13

              @StudentScripter
              You do see everything from your .ui being processed by uic. It is the ui_dialog.h you have included which contains that. That is all uic 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 the Ui 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 plain Dialog you are creating in your own .cpp & .h files, where you can write whatever code you like. The generated ui_....h file also contains a class name Dialog but it is in the Ui namespace. And the class member ui variable is an instance of that. Whose members (your widgets) are accessed via ui->..., so they do not interfere with anything you write in your Dialog 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.

              1 Reply Last reply
              1
              • S StudentScripter
                12 Mar 2024, 13:13

                @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;
                }
                
                

                cd8a8662-7ad4-4e11-a6a6-015a6fada3d2-image.png

                Sorry if we're talking past each other.

                P Offline
                P Offline
                Pl45m4
                wrote on 12 Mar 2024, 13:23 last edited by Pl45m4 3 Dec 2024, 13:28
                #14

                @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 your ui->pushbutton in "code", open the ui_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 the ui 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.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                S 1 Reply Last reply 12 Mar 2024, 15:50
                1
                • P Pl45m4
                  12 Mar 2024, 13:23

                  @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 your ui->pushbutton in "code", open the ui_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 the ui 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.

                  S Offline
                  S Offline
                  StudentScripter
                  wrote on 12 Mar 2024, 15:50 last edited by
                  #15

                  @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.

                  C J 2 Replies Last reply 12 Mar 2024, 16:03
                  0
                  • S StudentScripter
                    12 Mar 2024, 15:50

                    @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.

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 12 Mar 2024, 16:03 last edited by
                    #16

                    Then simply copy the stuff from setupUi() over to your ctor.

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

                    1 Reply Last reply
                    1
                    • S StudentScripter
                      12 Mar 2024, 15:50

                      @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.

                      J Offline
                      J Offline
                      JonB
                      wrote on 12 Mar 2024, 16:14 last edited by JonB 3 Dec 2024, 16:15
                      #17

                      @StudentScripter
                      Like I said, don't forget it's perfectly possible to look inside the ui_....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!

                      S 1 Reply Last reply 12 Mar 2024, 16:54
                      1
                      • J JonB
                        12 Mar 2024, 16:14

                        @StudentScripter
                        Like I said, don't forget it's perfectly possible to look inside the ui_....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!

                        S Offline
                        S Offline
                        StudentScripter
                        wrote on 12 Mar 2024, 16:54 last edited by
                        #18

                        @JonB Ok thanks well guess i can go in there and just copy what i need after beeing finished with prototyping and than delete the .ui file. :D Thanks man.

                        J 1 Reply Last reply 12 Mar 2024, 18:00
                        0
                        • S StudentScripter has marked this topic as solved on 12 Mar 2024, 16:54
                        • S StudentScripter
                          12 Mar 2024, 16:54

                          @JonB Ok thanks well guess i can go in there and just copy what i need after beeing finished with prototyping and than delete the .ui file. :D Thanks man.

                          J Offline
                          J Offline
                          JonB
                          wrote on 12 Mar 2024, 18:00 last edited by JonB 3 Dec 2024, 18:00
                          #19

                          @StudentScripter Yes, you can do that. And it shows you how they implemented stuff.

                          1 Reply Last reply
                          0

                          17/19

                          12 Mar 2024, 16:14

                          • Login

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