Call the. ui file

Source: Internet
Author: User

The first step is to make the UI file.
The first thing you should do is to draw your own interface with Qtdesigner, and Myform.ui (the MyForm here can be replaced with your favorite name).
There are a few key points to note when making your own interface files:
1. Remember the name of the UI file because the code generated by UIC exists in ui_myform.h
2. Remember the objectname of the main form, because the class name provided by the UI file is named after the form.
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
With this in mind, using your UI files in your code can be very straightforward.
Step two, add the UI file to the project
This step is the simplest, just need to modify the Pro file, add Forms+=myform.ui
The Qmake-project command can also identify files with a suffix called UI and add them to the project.
The third step is to reference the UI file in your 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 translated into standard C + + code and stored in an. h file, and this process is done after calling make, so you won't see the ui_myform.h file in the initial case, only the header file is generated after the make process. But it doesn't matter, we can still write the correct code without this file.
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 ();

};
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. Also, when writing code, be careful to refer to the "UI::" Method when using a class in a UI file.
See CPP file again
Myform.cpp
#include
#include "myform.h"

void My_function (void)

{Qmessagebox::information (this, "Name", Myui.lineeditname->text ());
}
Here you can write a function that shows how to invoke a control defined in a UI file in a form class. This piece of code is very simple, not much to explain.
With the foundation of single inheritance, learning to inherit more is a piece of cake. Take a look at the code to see it.
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
#include "myform.h"

void My_function (void)
{

Qmessagebox::information (This, "Name", Lineeditname->text ());
}
Does it not mean that we can understand it? Multi-inheritance is actually derived not only from the form class that is needed, but also by the class itself provided by the UI. The advantage of this is that your form class inherits all the controls and methods in the UI, so you can write fewer words when you call.
The two methods of single inheritance and multiple inheritance are not good or bad, we can choose according to their own programming habits.
The fourth step, compile, verify that the pro file contains the correct forms information in the case of running qmake; 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 Ui_myform.h file is generated, we recommend you to look at the contents of this file.

Call the. ui file

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.