Qt manually add UI files to the project

Source: Internet
Author: User
Tags qt designer

Making UI Files

You should first use QT Designer to draw an own interface, co-myform.ui (here MyForm can use their favorite name instead). There are a few key points to note when making your own interface files:

1, remember the name of the UI file, because the UIC generated code will exist in ui_myform.h;
2, to remember the main form of the object name, because the UI file provides the class name in this form to kill, for example, the main form name MainWindow, the UI file provides the class name is named Ui_mainwindow;
3. Pay special attention to the base class of your form selection to be compatible with the form class in your code;
4. Remember to give a meaningful and well-remembered object name to each control you need to access later, because the controls provided by the UI file will be named with these object name;

Adding a UI file to the project

1. Pro file already exists, modify Pro file, join Forms+=myform.ui

2. Run the Qmake-project command directly, the command is very smart, you can identify the suffix named. UI,. h,. cpp and other files, and add it to the project;

Referencing UI files in code

Official introduction of the use of the UI file methods have three kinds, one is direct reference, two is single inheritance, and three is multiple inheritance. The first method is actually very impractical, we have to look at the examples in the document can be, the second and the third kind of no essential difference, can and do a class, here to do the key introduction.

The UI file will eventually be compiled into standard C + + code and stored in an. h file, and this process does not occur until after call to make, so the ui_myform.h file is not visible in the initial case, only the header file is generated after the make process. But it doesn't matter, no such file can write the correct code.

The single-inheritance approach is simply to customize a subclass (later called MyForm) in the code, which derives from the form class (or its compatible subclasses) of forms, and defines the member variables in a class with the UI-generated class (later written Myui). In this way, the variables and functions in Myui and Myui can be called directly in the MyForm constructor, which is convenient to use. For example, here is a UI file called Myform.ui, the UI file is defined in the form named Bigwidget, with a single-line edit control called Lineeditname:

Myform.h   #include "ui_myform.h"   class Myform:public Qwidget  {  q_object public  :      MyForm ( Qwidget*parent)      {      myui.setupui (this);      }  Private:      ui::bigwidget Myui;  Private:      void My_function ();  };  

Myform.h  #include "ui_myform.h"  class Myform:public Qwidget  {  q_object public  :      MyForm (qwidget*parent)      {      myui.setupui (this);      }  Private:      ui::bigwidget Myui;  Private:      void My_function ();  };  

The above simple class declaration is the best example of the first three points mentioned above, please understand the meaning of the text description and the specific code of the key points. Another interesting point here is that the UI file provides classes that are included in the name space named UI, which is designed to separate the UI file namespace from the user's code and avoid naming conflicts between the two. Accordingly, when we write the code, we should also note that when using a class in the UI file, you use the "UI::" method to refer to it.

Myform.cpp   #include <QMessageBox>   #include "myform.h"   void my_function (void)  {      Qmessagebox::information (This, "Name", Myui.lineeditname->text ());  }  

  

Myform.cpp  #include <QMessageBox>  #include "myform.h"  void my_function (void)  {      Qmessagebox::information (This, "Name", Myui.lineeditname->text ());  }  

The above is a single-inheritance form, the main most commonly used or multi-inheritance, using public inheritance, you can access the parts of the bigwidget outside the serial parts, the advantage is that your form class inherits all the controls and methods in the UI, note that you can not miss the Q_object macro at this time.

Myform.h   #include "ui_myform.h"   class Myform:public Qwidget, public ui::bigwidget  {  q_object  Public:  MyForm (qwidget*parent)  {  setupui (this);  }  Private:  void My_function ();  };  Myform.cpp   #include <QMessageBox>   #include "myform.h"   void my_function (void)  {      Qmessagebox::information (This, "Name", Lineeditname->text ());  }  

  

Myform.h  #include "ui_myform.h"  class Myform:public Qwidget, public ui::bigwidget  {  q_object Public  :  MyForm (qwidget*parent)  {  setupui (this);  }  Private:  void My_function ();  };  Myform.cpp  #include <QMessageBox>  #include "myform.h"  void my_function (void)  {      Qmessagebox::information (This, "Name", Lineeditname->text ());  }  

Compile

1. Run Qmake in case the pro file contains the correct forms information; Make will be able to compile the project. Make when you take a look at the output you will find that make will automatically call UIC to generate the required code at the start of the compilation. After make, the ui_myform.h file is generated;

2. Run the command prompt line to the project directory, run Qmake-project-t vcapp-o ***.pro, generate Pro file and run Qmake again;

3. The project files required in this case are generated.

Qt manually add UI files to the project (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.