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

How to create .ui file for manually edited application (.h & .cpp only)?



  • I have Qt 5.5 Creator and Qt 5.5 Designer working in Ubuntu 14.04.
    I have walked through creation of several example projects which generate *.ui file per project allowing Qt objects to be edited in Qt Creator.

    Next I explored a manually coded test application here ...

    http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html

    This uses .h and .cpp and image files manually edited and placed in project folder.

    Files:
    mainwindow.cpp
    mainwindow.h
    main.cpp
    application.pro
    application.qrc

    Images:
    images/copy.png
    images/cut.png
    images/new.png
    images/open.png
    images/paste.png
    images/save.png

    But there is no *.ui file in the project since this was not created through Qt Creator.
    I can open this C++ project in QtCreator and Run to see the working app.

    My first question is ..

    Q. How can I create a *.ui file from a manually edited app (as above) so that it can be edited in Qt Creator and extended?

    While there is a process for .h and .cpp to create .ui, it seems that there is no tool I've found to reverse engineer this process. Although I read through here ..

    http://stackoverflow.com/questions/16004235/qt-generate-ui-file-from-old-c-project

    ... ...

    I thought to get around this I might be able to create a blank widget container in Qt Creator and import the above test C++ app. But I don't see how to do this in Qt Creator .. integrating external coded apps.

    My second question is ..

    Q. How do I integrate external .h and .cpp files (external app) into a Qt Creator created project?

    I found this discussion.

    http://www.qtcentre.org/threads/56441-How-to-use-external-source-code-in-Qt-creator/page2


  • Moderators

    How can I create a *.ui file from a manually edited app

    • In Qt Creator go to File -> New File or Project -> Qt -> Qt Designer Form. Select a widget type, e.g. Widget and give the file a meaningful name (usually the same as your h/cpp file): mainwindow.ui. It will be added to you project.
    • Open it in the designer, select the widget and set its property objectName to match the name of your class in your h/cpp (it's not necessary but recommended to keep things in order).
    • In your .h file add:
    namespace Ui {
    class MainWindow; //this is the property `objectName` from the previous step. They need to match
    }
    

    and then a private field in your class declaration:

    class MainWindow : 
    ...
    private:
       Ui::MainWindow* ui; //this is again the matching name
    }
    
    • in your .cpp file include the header generated by uic: #include "ui_mainwindow.h". the mainwindow part is the name of your .ui file.
    • in your constructor create the ui instance and setup:
    MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
       ui->setupUi(this);
    }
    
    • in the destructor destroy the ui instance:
    MainWindow::~MainWindow() {
       delete ui;
    }
    

    How do I integrate external .h and .cpp files (external app)

    just open your .pro file and add them to HEADERS and SOURCES variables.


Log in to reply