QT (3) objects and inheritance-addressbook Example 1

Source: Internet
Author: User

After a few days of cold, this afternoon a glimmer of sunshine.

The last time we learned how to construct a simple example, since QT is a C + + based, we need to know how to create an object and implement an object's inheritance, while we learn about the qgridlayout in layout.

In the last example, we know that if you create a qwidget with no parent, the widget is considered a window. Now, we create such a Windows class, place this window as a panel with various components, so the class inherits Qwidget. The class name is AddressBook, so create two file addressbook.cpp,addressbook.h and the main function qtmain.cpp.

Related reference: http://doc.qt.nokia.com/latest/tutorials-addressbook-part1.html

In this addressbook windows, we use the guidlayout way to emit, the following figure is the final execution result, the left figure is in the handset simulator operation, the right figure on the Ubuntu operation, and adds the component placement position.

First, write *.pro documents

#和以前的例子像比较, add target defaults to the name of the *.pro file, if different, redefine it, and add the required header file and source code file as sources and headers two parameters
TARGET = Local-test2
SOURCES = qtmain.cpp addressbook.cpp
HEADERS = Addressbook.h

VPATH = src
Objects_dir = Build
Destdir = Build

second, the object's 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>
/* Since we used the Qlineedit and qtextedit two classes in AddressBook, not the qwidget definition, so in order to ensure that the compiler passes, it needs to be defined here (feel somewhat like an extern definition inside C), Otherwise, it will be reported at compile time: ISO C + + forbids declaration of ' qlineedit ' with no type. If #include <qtgui>, this is OK without this two, but a moc_addressbook.cpp file will be generated. */
Class Qlineedit;
Class Qtextedit;

Class AddressBook: Public qwidget //Note how inheritance is written
/* We use the Q_obect macro definition to show that the class uses QT's many excellent features, including signal and slots, and so on. Even if we do not use these features and features, we still recommend that we define them. In our superficial study, we can simply assume that the Q_object macro definition is a shortcut.*/of the TR () and connect () functions that allow us to use QT.

AddressBook (qwidget * parent = NULL);

Qlineedit * Nameline;
Qtextedit * ADDRESSTEXT;


third, 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)//integration constructor, and note the inherited writing format.
Qlabel * Namelabel = new Qlabel (tr ("Name:"));
Nameline = new Qlineedit;

Qlabel * Addresslabel = new Qlabel (tr ("Address:"));
Addresstext = new Qtextedit;

This article describes the use of GridLayout layout, see the right above, the panel is divided into two (x,y) two-dimensional space, in each location using AddWidget for typesetting. In addition, if there are no parameters, we see the 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); Because it is a qwidget inheritance, you can use the setlayout directly
Setwindowtitle (tr("Simple Address Book")); The use of TR (), which is Q_object, is not known in this translate, and can be converted as a coded format.

main file main ()

/* qtmain.cpp-the file containing a main () function with a instance of AddressBook. */
#include <QtGui>
#include "Addressbook.h"

int main (int argc, char * argv[])
Qapplication app (ARGC,ARGV);

AddressBook AddressBook;
Addressbook.show ();

return App.exec ();

RELATED links: My Meego/moblin related articles

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.