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. convert .ui to .h and .cpp?

convert .ui to .h and .cpp?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 2.3k Views 3 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.
  • S Offline
    S Offline
    s002wjh
    wrote on last edited by kshegunov
    #1

    how do I convert a .ui file to .h and .cpp file? qmake? or...

    kshegunovK 1 Reply Last reply
    0
    • S s002wjh

      how do I convert a .ui file to .h and .cpp file? qmake? or...

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @s002wjh
      uic to convert .ui to .h. There's no user interface compilation to .cpp.

      Read and abide by the Qt Code of Conduct

      M 1 Reply Last reply
      3
      • S Offline
        S Offline
        s002wjh
        wrote on last edited by
        #3

        ok thanks, I thought I watch youtube someone use qtcreator to convert pushbutton created in gui UI, and generate .cpp

        1 Reply Last reply
        0
        • kshegunovK kshegunov

          @s002wjh
          uic to convert .ui to .h. There's no user interface compilation to .cpp.

          M Offline
          M Offline
          Mr-Workalot
          wrote on last edited by
          #4

          @kshegunov , I did this and have the .h file now, How do i execute it, sorry im really noob in this,
          initially i converted .ui file to .py file and was able to run it in python, but how do i do it in c++, i only have the .h file.

          mrjjM 1 Reply Last reply
          0
          • M Mr-Workalot

            @kshegunov , I did this and have the .h file now, How do i execute it, sorry im really noob in this,
            initially i converted .ui file to .py file and was able to run it in python, but how do i do it in c++, i only have the .h file.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Mr-Workalot
            Hi and welcome to the forums.

            Normally the .h file defines a struct that holds all the QWidget pointers
            and you can have an instance of this and pass the parent to the
            void setupUi(QMainWindow *MainWindow) function.

            However, if the UI is not very complex, you could also cheat and
            1: create a default GUI project
            2: open the original UI file in Creator and select and copy all
            3: Paste it to the new blank ui file in the default project

            That way you can use it more normally than starting from a .h file :)

            That said, if it has many layouts etc, it might not paste 100% correctly

            There is also the option to use
            https://doc.qt.io/qt-5/quiloader.html
            However, that wont give easy access to the widgets so better for other use cases.

            Could you paste the .h file here ?
            just want to be sure you have the same as normal since i never used uic manually
            so not 100% sure its the same as the default GUI project that comes with ui/h/cpp

            M 1 Reply Last reply
            1
            • mrjjM mrjj

              @Mr-Workalot
              Hi and welcome to the forums.

              Normally the .h file defines a struct that holds all the QWidget pointers
              and you can have an instance of this and pass the parent to the
              void setupUi(QMainWindow *MainWindow) function.

              However, if the UI is not very complex, you could also cheat and
              1: create a default GUI project
              2: open the original UI file in Creator and select and copy all
              3: Paste it to the new blank ui file in the default project

              That way you can use it more normally than starting from a .h file :)

              That said, if it has many layouts etc, it might not paste 100% correctly

              There is also the option to use
              https://doc.qt.io/qt-5/quiloader.html
              However, that wont give easy access to the widgets so better for other use cases.

              Could you paste the .h file here ?
              just want to be sure you have the same as normal since i never used uic manually
              so not 100% sure its the same as the default GUI project that comes with ui/h/cpp

              M Offline
              M Offline
              Mr-Workalot
              wrote on last edited by
              #6

              @mrjj Thanks so much for reply, Actually i used the QTDesigner and created the finalversion_with_buttons.ui file and then got the .h file by using
              uic -o finalversion_with_buttons.h finalversion_with_buttons.ui in cmd prompt,,
              i don't know what to do next to get the .cpp file???, i have tried QT Creator but it looks too confusing.

              M Pl45m4P 2 Replies Last reply
              0
              • M Mr-Workalot

                @mrjj Thanks so much for reply, Actually i used the QTDesigner and created the finalversion_with_buttons.ui file and then got the .h file by using
                uic -o finalversion_with_buttons.h finalversion_with_buttons.ui in cmd prompt,,
                i don't know what to do next to get the .cpp file???, i have tried QT Creator but it looks too confusing.

                M Offline
                M Offline
                Mr-Workalot
                wrote on last edited by
                #7

                This is finalversion_with_buttons.h ,, its a complex UI , not a basic one

                /********************************************************************************
                ** Form generated from reading UI file 'finalversion_with_buttons.ui'
                **
                ** Created by: Qt User Interface Compiler version 5.13.2
                **
                ** WARNING! All changes made in this file will be lost when recompiling UI file!
                ********************************************************************************/

                #ifndef FINALVERSION_WITH_BUTTONS_H
                #define FINALVERSION_WITH_BUTTONS_H

                #include <QtCore/QVariant>
                #include <QtWidgets/QAction>
                #include <QtWidgets/QApplication>
                #include <QtWidgets/QFrame>
                #include <QtWidgets/QGridLayout>
                #include <QtWidgets/QLabel>
                #include <QtWidgets/QMainWindow>
                #include <QtWidgets/QMenu>
                #include <QtWidgets/QMenuBar>
                #include <QtWidgets/QPushButton>
                #include <QtWidgets/QStatusBar>
                #include <QtWidgets/QTextEdit>
                #include <QtWidgets/QWidget>

                QT_BEGIN_NAMESPACE

                class Ui_MainWindow
                {
                public:
                QAction *actionNew;
                QAction *actionCopy;
                QAction *actionPaste;
                QAction *actionopen;
                QAction *actionSave_2;
                QWidget *centralwidget;
                QGridLayout *gridLayout_2;
                QLabel *Xml_Script_label;
                QTextEdit *Cmd_buffer_textbox;
                QLabel *Variable_label;
                QTextEdit *UI_Sketch_textbox;
                QLabel *Command_buffer_label;
                QPushButton *playbutton;
                QFrame *line;
                QGridLayout *gridLayout;
                QLabel *UI_sketch_label;
                QTextEdit *Xml_Script_textbox;
                QPushButton *Save_button;
                QTextEdit *Execution_sketch_textbox;
                QPushButton *nextstepbutton;
                QTextEdit *Variable_textbox;
                QLabel *Execution_Sketch_label;
                QPushButton *Open_button;
                QMenuBar *menubar;
                QMenu *menuEdit;
                QMenu *menuHelp;
                QMenu *menuSettings;
                QMenu *menuHelp_2;
                QStatusBar *statusbar;

                void setupUi(QMainWindow *MainWindow)
                {
                    if (MainWindow->objectName().isEmpty())
                        MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
                    MainWindow->resize(800, 458);
                    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                    sizePolicy.setHorizontalStretch(0);
                    sizePolicy.setVerticalStretch(0);
                    sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth());
                    MainWindow->setSizePolicy(sizePolicy);
                    MainWindow->setMinimumSize(QSize(0, 285));
                    actionNew = new QAction(MainWindow);
                    actionNew->setObjectName(QString::fromUtf8("actionNew"));
                    actionCopy = new QAction(MainWindow);
                    actionCopy->setObjectName(QString::fromUtf8("actionCopy"));
                    actionPaste = new QAction(MainWindow);
                    actionPaste->setObjectName(QString::fromUtf8("actionPaste"));
                    actionopen = new QAction(MainWindow);
                    actionopen->setObjectName(QString::fromUtf8("actionopen"));
                    actionSave_2 = new QAction(MainWindow);
                    actionSave_2->setObjectName(QString::fromUtf8("actionSave_2"));
                    centralwidget = new QWidget(MainWindow);
                    centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
                    gridLayout_2 = new QGridLayout(centralwidget);
                    gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));
                    Xml_Script_label = new QLabel(centralwidget);
                    Xml_Script_label->setObjectName(QString::fromUtf8("Xml_Script_label"));
                    QFont font;
                    font.setPointSize(12);
                    Xml_Script_label->setFont(font);
                
                    gridLayout_2->addWidget(Xml_Script_label, 3, 0, 1, 1);
                
                    Cmd_buffer_textbox = new QTextEdit(centralwidget);
                    Cmd_buffer_textbox->setObjectName(QString::fromUtf8("Cmd_buffer_textbox"));
                    Cmd_buffer_textbox->setBaseSize(QSize(256, 251));
                
                    gridLayout_2->addWidget(Cmd_buffer_textbox, 4, 3, 4, 2);
                
                    Variable_label = new QLabel(centralwidget);
                    Variable_label->setObjectName(QString::fromUtf8("Variable_label"));
                    Variable_label->setFont(font);
                
                    gridLayout_2->addWidget(Variable_label, 6, 5, 1, 1);
                
                    UI_Sketch_textbox = new QTextEdit(centralwidget);
                    UI_Sketch_textbox->setObjectName(QString::fromUtf8("UI_Sketch_textbox"));
                    QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding);
                    sizePolicy1.setHorizontalStretch(250);
                    sizePolicy1.setVerticalStretch(150);
                    sizePolicy1.setHeightForWidth(UI_Sketch_textbox->sizePolicy().hasHeightForWidth());
                    UI_Sketch_textbox->setSizePolicy(sizePolicy1);
                    UI_Sketch_textbox->setMaximumSize(QSize(250, 16777215));
                
                    gridLayout_2->addWidget(UI_Sketch_textbox, 2, 5, 1, 1);
                
                    Command_buffer_label = new QLabel(centralwidget);
                    Command_buffer_label->setObjectName(QString::fromUtf8("Command_buffer_label"));
                    Command_buffer_label->setFont(font);
                
                    gridLayout_2->addWidget(Command_buffer_label, 3, 3, 1, 2);
                
                    playbutton = new QPushButton(centralwidget);
                    playbutton->setObjectName(QString::fromUtf8("playbutton"));
                    QFont font1;
                    font1.setPointSize(10);
                    playbutton->setFont(font1);
                
                    gridLayout_2->addWidget(playbutton, 1, 3, 2, 1);
                
                    line = new QFrame(centralwidget);
                    line->setObjectName(QString::fromUtf8("line"));
                    line->setFrameShape(QFrame::HLine);
                    line->setFrameShadow(QFrame::Sunken);
                
                    gridLayout_2->addWidget(line, 8, 0, 1, 6);
                
                    gridLayout = new QGridLayout();
                    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
                    gridLayout->setSizeConstraint(QLayout::SetNoConstraint);
                
                    gridLayout_2->addLayout(gridLayout, 0, 0, 1, 1);
                
                    UI_sketch_label = new QLabel(centralwidget);
                    UI_sketch_label->setObjectName(QString::fromUtf8("UI_sketch_label"));
                    UI_sketch_label->setFont(font);
                
                    gridLayout_2->addWidget(UI_sketch_label, 1, 5, 1, 1);
                
                    Xml_Script_textbox = new QTextEdit(centralwidget);
                    Xml_Script_textbox->setObjectName(QString::fromUtf8("Xml_Script_textbox"));
                    Xml_Script_textbox->setBaseSize(QSize(256, 251));
                    Xml_Script_textbox->setAutoFillBackground(true);
                
                    gridLayout_2->addWidget(Xml_Script_textbox, 4, 0, 4, 3);
                
                    Save_button = new QPushButton(centralwidget);
                    Save_button->setObjectName(QString::fromUtf8("Save_button"));
                    Save_button->setFont(font1);
                
                    gridLayout_2->addWidget(Save_button, 1, 2, 2, 1);
                
                    Execution_sketch_textbox = new QTextEdit(centralwidget);
                    Execution_sketch_textbox->setObjectName(QString::fromUtf8("Execution_sketch_textbox"));
                    sizePolicy1.setHeightForWidth(Execution_sketch_textbox->sizePolicy().hasHeightForWidth());
                    Execution_sketch_textbox->setSizePolicy(sizePolicy1);
                    Execution_sketch_textbox->setMaximumSize(QSize(250, 16777215));
                
                    gridLayout_2->addWidget(Execution_sketch_textbox, 5, 5, 1, 1);
                
                    nextstepbutton = new QPushButton(centralwidget);
                    nextstepbutton->setObjectName(QString::fromUtf8("nextstepbutton"));
                    nextstepbutton->setFont(font1);
                
                    gridLayout_2->addWidget(nextstepbutton, 1, 4, 2, 1);
                
                    Variable_textbox = new QTextEdit(centralwidget);
                    Variable_textbox->setObjectName(QString::fromUtf8("Variable_textbox"));
                    sizePolicy1.setHeightForWidth(Variable_textbox->sizePolicy().hasHeightForWidth());
                    Variable_textbox->setSizePolicy(sizePolicy1);
                    Variable_textbox->setMaximumSize(QSize(250, 16777215));
                
                    gridLayout_2->addWidget(Variable_textbox, 7, 5, 1, 1);
                
                    Execution_Sketch_label = new QLabel(centralwidget);
                    Execution_Sketch_label->setObjectName(QString::fromUtf8("Execution_Sketch_label"));
                    Execution_Sketch_label->setFont(font);
                
                    gridLayout_2->addWidget(Execution_Sketch_label, 3, 5, 2, 1);
                
                    Open_button = new QPushButton(centralwidget);
                    Open_button->setObjectName(QString::fromUtf8("Open_button"));
                    Open_button->setFont(font1);
                
                    gridLayout_2->addWidget(Open_button, 1, 1, 2, 1);
                
                    MainWindow->setCentralWidget(centralwidget);
                    menubar = new QMenuBar(MainWindow);
                    menubar->setObjectName(QString::fromUtf8("menubar"));
                    menubar->setGeometry(QRect(0, 0, 800, 18));
                    menuEdit = new QMenu(menubar);
                    menuEdit->setObjectName(QString::fromUtf8("menuEdit"));
                    menuHelp = new QMenu(menubar);
                    menuHelp->setObjectName(QString::fromUtf8("menuHelp"));
                    menuSettings = new QMenu(menubar);
                    menuSettings->setObjectName(QString::fromUtf8("menuSettings"));
                    menuHelp_2 = new QMenu(menubar);
                    menuHelp_2->setObjectName(QString::fromUtf8("menuHelp_2"));
                    MainWindow->setMenuBar(menubar);
                    statusbar = new QStatusBar(MainWindow);
                    statusbar->setObjectName(QString::fromUtf8("statusbar"));
                    MainWindow->setStatusBar(statusbar);
                
                    menubar->addAction(menuEdit->menuAction());
                    menubar->addAction(menuHelp->menuAction());
                    menubar->addAction(menuSettings->menuAction());
                    menubar->addAction(menuHelp_2->menuAction());
                    menuEdit->addSeparator();
                    menuEdit->addAction(actionCopy);
                    menuEdit->addAction(actionPaste);
                
                    retranslateUi(MainWindow);
                
                    QMetaObject::connectSlotsByName(MainWindow);
                } // setupUi
                
                void retranslateUi(QMainWindow *MainWindow)
                {
                    MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "UI Tool", nullptr));
                    actionNew->setText(QCoreApplication::translate("MainWindow", "New", nullptr));
                

                #if QT_CONFIG(statustip)
                actionNew->setStatusTip(QCoreApplication::translate("MainWindow", "Create a New File", nullptr));
                #endif // QT_CONFIG(statustip)
                #if QT_CONFIG(shortcut)
                actionNew->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+N", nullptr));
                #endif // QT_CONFIG(shortcut)
                actionCopy->setText(QCoreApplication::translate("MainWindow", "Copy", nullptr));
                #if QT_CONFIG(statustip)
                actionCopy->setStatusTip(QCoreApplication::translate("MainWindow", "Copy a File", nullptr));
                #endif // QT_CONFIG(statustip)
                #if QT_CONFIG(shortcut)
                actionCopy->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+C", nullptr));
                #endif // QT_CONFIG(shortcut)
                actionPaste->setText(QCoreApplication::translate("MainWindow", "Paste", nullptr));
                #if QT_CONFIG(statustip)
                actionPaste->setStatusTip(QCoreApplication::translate("MainWindow", "Paste a file", nullptr));
                #endif // QT_CONFIG(statustip)
                #if QT_CONFIG(shortcut)
                actionPaste->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+V", nullptr));
                #endif // QT_CONFIG(shortcut)
                actionopen->setText(QCoreApplication::translate("MainWindow", "Open", nullptr));
                #if QT_CONFIG(statustip)
                actionopen->setStatusTip(QCoreApplication::translate("MainWindow", "Open an XML File", nullptr));
                #endif // QT_CONFIG(statustip)
                #if QT_CONFIG(shortcut)
                actionopen->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+S", nullptr));
                #endif // QT_CONFIG(shortcut)
                actionSave_2->setText(QCoreApplication::translate("MainWindow", "Save", nullptr));
                Xml_Script_label->setText(QCoreApplication::translate("MainWindow", "XML Script", nullptr));
                Variable_label->setText(QCoreApplication::translate("MainWindow", "Variable", nullptr));
                Command_buffer_label->setText(QCoreApplication::translate("MainWindow", "Command Buffer", nullptr));
                playbutton->setText(QCoreApplication::translate("MainWindow", "Play", nullptr));
                UI_sketch_label->setText(QCoreApplication::translate("MainWindow", "UI Sketch", nullptr));
                Save_button->setText(QCoreApplication::translate("MainWindow", "Save", nullptr));
                nextstepbutton->setText(QCoreApplication::translate("MainWindow", "Next Step", nullptr));
                Execution_Sketch_label->setText(QCoreApplication::translate("MainWindow", "Execution Sketch", nullptr));
                Open_button->setText(QCoreApplication::translate("MainWindow", "Open", nullptr));
                menuEdit->setTitle(QCoreApplication::translate("MainWindow", "File", nullptr));
                menuHelp->setTitle(QCoreApplication::translate("MainWindow", "Help", nullptr));
                menuSettings->setTitle(QCoreApplication::translate("MainWindow", "Settings", nullptr));
                menuHelp_2->setTitle(QCoreApplication::translate("MainWindow", "Help", nullptr));
                } // retranslateUi

                };

                namespace Ui {
                class MainWindow: public Ui_MainWindow {};
                } // namespace Ui

                QT_END_NAMESPACE

                #endif // FINALVERSION_WITH_BUTTONS_H

                1 Reply Last reply
                0
                • M Mr-Workalot

                  @mrjj Thanks so much for reply, Actually i used the QTDesigner and created the finalversion_with_buttons.ui file and then got the .h file by using
                  uic -o finalversion_with_buttons.h finalversion_with_buttons.ui in cmd prompt,,
                  i don't know what to do next to get the .cpp file???, i have tried QT Creator but it looks too confusing.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @Mr-Workalot said in convert .ui to .h and .cpp?:

                  i don't know what to do next to get the .cpp file???

                  What *.cpp are you talking about? You have to write your logic by yourself. You can not design a cpp-file with UI designer. As @kshegunov said 3 1/2 years ago, there is no translation from UI to cpp. The User Interface Compiler (UIC) generates a header to provide all the widgets and settings, you've created or edited with QtDesigner (your UI file).

                  Tested your header, worked for me. So I guess, you can load it with UiLoader or something else to get your GUI.

                  btw finalversion_with_buttons.ui is a horrible name for an UI-file inside a project ;-)


                  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
                  3

                  • Login

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