Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

convert .ui to .h and .cpp?



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


  • Moderators

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


  • Moderators

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



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



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


  • Lifetime Qt Champion

    @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



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



  • 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



  • @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 ;-)


Log in to reply