After a few days, the sun was shining this afternoon.
We learned how to construct a simple example last time. Since QT is C ++ based, we need to know how to create an object and implement Object Inheritance, we also learn QGridLayout in Layout.
In the previous example, we know that if we create a QWidget without parent, This widget is regarded as a window. Now, we create such a windows class and place the window as a panel on it with various components. Therefore, this class inherits QWidget. This class is named Addressbook. Therefore, you can create two files addressbook. cpp, addressbook. h, and qtmain. cpp where the main function is located.
Reference: http://doc.qt.nokia.com/latest/tutorials-addressbook-part1.html
On this Addressbook windows, we use the GuidLayout Method for emissions. This is the final execution result. The picture on the left is running on the Handset simulator, and the picture on the right is running on ubuntu, the component placement position is added.
1. Write the *. pro File
# Compared with the previous example, the default TARGET value * is added *. the name of the pro file, if different, needs to be redefined, and the header file and source code file to be compiled must be supplemented with two parameters, such as SOURCES and HEADERS.
TARGET = local-test2
SOURCES =Qtmain. cpp addressbook. cpp
HEADERS =Addressbook. h
VPATH = src
OBJECTS_DIR = build
DESTDIR = build
Ii. object header file: addressbook. h
/* Addressbook. h-defines the AddressBook class as an inherited subclass of QWidget and defines a constructor. */
# Ifndef COM_WEI_ADDRESSBOOK_H
# Define COM_WEI_ADDRESSBOOK_H
# Include <QWidget>
/* Because we use QLineEdit and QTextEdit classes in AddressBook, they are not within the definition scope of QWidget. Therefore, to ensure that the compilation passes, you need to define it here (it feels like the extern definition in C). Otherwise, it will be reported during compilation: In iso c ++ forbids declaration of 'qlineedit 'with no type. If the # include <QtGui> clause is not added, a moc_addressbook.cpp file is generated. */
Class QLineEdit;
Class QTextEdit;
Class AddressBook:Public qwidget// Pay attention to the inherited Writing Method
{
/* We use the Q_OBECT macro definition to show that this class uses many excellent features of QT, including signal and slots. Even if we do not use these features and features, we recommend that you define them. In our superficial learning, we can simply think that the definition of the Q_OBJECT macro allows us to use the objective cut of the tr () and connect () functions of QT .*/
Q_object
Public:
AddressBook (QWidget * parent = NULL );
Private:
QLineEdit * nameLine;
QTextEdit * addressText;
};
# Endif
Iii. Object source file: addressbook. cpp
/* Addressbook. cpp-the implementation file for the AddressBook class */
# Include <QtGui>
# Include "addressbook. h"
AddressBook ::Addressbook(QWidget * parent ):Qwidget(Parent)
// Integrate the constructor and pay attention to the inherited writing format.
{
QLabel * nameLabel = new QLabel (tr ("Name :"));
NameLine = new QLineEdit;
QLabel * addressLabel = new QLabel (tr ("Address :"));
AddressText = new QTextEdit;
// The Layout Method of GridLayout is introduced here. See the figure on the right above. Divide the panel into two-dimensional spaces (x, y) and use addWidget for layout at each position. If no parameter exists, we can see that new QGridLayout () can also be written as new QGridLayout.
QGridLayout * mainLayout = new QGridLayout;
MainLayout-> addWidget (nameLabel, 0, 0 );
MainLayout-> addWidget (nameLine, 0, 1 );
MainLayout-> addWidget (addressLabel, 1, 0, Qt: AlignTop );
MainLayout-> addWidget (addressText, 1, 1 );
SetLayout (mainLayout); // you can directly use setLayout because it is inherited by QWidget.
SetWindowTitle (Tr("Simple Address Book"); // tr () is used here, that is, Q_OBJECT. The usage of this translate is unknown and can be used as the conversion of the encoding format.
}
4. main file main ()
/* Qtmain. cpp-the file containing a main () function, with an instance of AddressBook .*/
# Include <qtgui>
# Include "addressbook. H"
Int main (INT argc, char * argv [])
{
Qapplication app (argc, argv );
Addressbook;
Addressbook. Show ();
Return app.exe C ();
}
Related Links: My MeeGo/Moblin articles