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. Ui files
Qt 6.11 is out! See what's new in the release blog

Ui files

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 4.1k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    ? 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      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. :-)

      ? 1 Reply Last reply
      0
      • ? A Former User

        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. :-)

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

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

        ? 1 Reply Last reply
        0
        • ? A Former User

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

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

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

          ? 1 Reply Last reply
          0
          • ? A Former User

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

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

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

            kshegunovK 1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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. :-)

              1 Reply Last reply
              0
              • ? A Former User

                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!

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by A Former User
                #7

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

                1 Reply Last reply
                0
                • ? A Former User

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

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

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

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0

                  • Login

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