Ui files



  • QT: 4.8.6
    Visual Studio 2008

    Hello everyone.
    I'm on a project that does not use .ui files.
    But I'm spending my time trying align the widgets and I want learn how to use ui files.

    I have the QT Designer, and I know that I need put the ui filename inside the .pro file in FORMS +=
    and add the ui_filename.h inside the class that it will be used.

    But how can I call the ui file and show it?

    My best regards!



  • Hi! Maybe you want to put aside Visual Studio for a while and try out Qt Creator. It integrates Qt Designer and a nice code editor. It also automatically modifies your *.pro file when you're adding or removing *.ui files. So it'll make your life much easier. :-)



  • @Wieland
    Thank you, but there is a way to achieve this in VS ?



  • @Helson Sure there is. Do you have installed the Qt plugin for VS?



  • @Wieland
    Yes, I can open .pro files.
    VS has the Qt menu.



  • Ok. So, did you already create a main window with Qt Designer? If so, you should now have a file called mainwindow.ui. You need to add it to your project file with FORMS += mainwindow.ui. During the build process qmake will create a file called ui_mainwindow.h from this *.ui file. The next step is that you need to create two new files: mainwindow.h and mainwindow.cpp. They should look like this:

    mainwindow.h :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp :

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    Now you're almost done. In your main.cpp you'll include "mainwindow.h", create an instance of it and show it:

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    Now run qmake, build your project and try to run it. :-)



  • @Helson said:

    But how can I call the ui file and show it?

    About the call part, I think that @Wieland answered it. But by "show it" do you mean that you want VS to open your .ui file the same way Qt Designer does? With WYSIWYG interface, listing every available widget, apply layouts and stuff? If so, AFAIK, it won't be possible: you will have to edit your .ui file on Qt Designer, save it, and code in VS. Hence the comment from @Wieland about using QtCreator only and putting aside VS.


  • Qt Champions 2016

    @Helson
    Hello,
    @Wieland has provided a great starting point. There are 3 other ways to use .ui forms depending on your use case. One is to load them dynamically, although it's a bit more involved than relying on the uic for Ui class generation. You could also inherit from the Ui class (instead of aggregating an instance), however I dislike the concept on principle. Additionally a widget could be initialized "from the outside" that would be my preferred way to use forms, like this:

    QMainWindow window;
    Ui::MainWindowForm ui;
    
    ui.setupUi(&window);  // < Initialize QMainWindow with the form 
    window.show();        // < Show the main window
    

    However I would not recommend it for a beginner. In your case the most painless way to use forms is what @Wieland suggested. After you call setupUi with the widget you're initializing you can access the combo boxes, edit boxes and other widgets through the Ui class' instance. I hope this helps.

    Kind regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.