QT4 Reading notes 5

Source: Internet
Author: User
Tags emit line editor qt designer

QT4 Reading notes 5

Chapter II Creating a dialog box

This chapter will teach you how to create a dialog box with Qt. The dialog box presents the user with some options (options and Choices), which run the user to set these options. This is called a dialog box (dialog boxes,or simply "dialogs"), which provides a way for users and applications to talk to each other.

Many GUI applications include a main window (a main Windows) and a menu, a toolbar, and a dozen dialog boxes. Of course, you can also do a dialog-only application, such as a calculator.

We will create a purely dialog box. Then see how to create a dialog box using Qt Designer. Use QT Designer to encode images manually and easily modify them.

Subclass of Qdialogde

The source code is divided into two files: Finddialog.h and Finddialog.cpp, let's see Finddialog.h first.

#ifndef Finddialog_h

#define Finddialog_h

Protect header files, avoid multiple inclusions

#include <QDialog>

Qdialog class inherits from Qwidge

Class Qcheckbox;

Class Qlabel;

Class Qlineedit;

Class Qpushbutton;

The QT class used later in the declaration. Forward declaration is a typical practice of C + +.

Then we define finddialog as a subclass of Qdialog.

Class Finddialog:public Qdialog

{

Q_object

Public

Finddialog (Qwidget *parent = 0);

The Q_object macro must be defined at the beginning of the class, which is required to use the signals or slot.

The Finddialog constructor method is typical, and the parent parameter indicates its parental control. The default is null, which means there is no parent control for this dialog box.

Signals:

void FindNext (const QString &STR, qt::casesensitivity CS);

void findprevious (const QString &STR, qt::casesensitivity CS);

The signals zone declares two signal, and when the user taps the Find button, the dialog box emits these two signal. If the search backward option is enabled, the dialog box emits findprevious (), otherwise it emits findnext ().

The Signals keyword is actually a macro. The C + + preprocessor translates it into standard C + +. Qt::casesensitivity is an enum type with two values: Qt::casesensitive and Qt::caseinsensitive

Private Slots:

void findclicked ();

void Enablefindbutton (const QString &text);

Private

Qlabel *label;

Qlineedit *lineedit;

Qcheckbox *casecheckbox;

Qcheckbox *backwardcheckbox;

Qpushbutton *findbutton;

Qpushbutton *closebutton;

};

#endif

Defines two slot.slots is also a macro

Let's take a look at Finddialog.cpp, this file is the implementation of the Finddialog class

#include <QtGui>

#include "Finddialog.h"

First contains the <qtgui>, and this header file includes the definition of the Qt GUI class. QT contains many modules, each with its own library. The most important modules are Qtcore, Qtgui,qtnetwork, Qtopengl, Qtscript, Qtsql, Qtsvg, qtxml. <QtGui> header files include all classes of Qtcore and Qtgui modules. This avoids our own search for the included classes and reduces the hassle.

Finddialg::finddialog (Qwidget *parent): Qdialog (parent)

{

label = new Qlabel (tr ("Find &what:"));

LineEdit = new Qlineedit;

Label->setbuddy (LineEdit);

Casecheckbox = new Qcheckbox (tr ("Match &case"));

Backwardcheckbox = new Qcheckbox (tr ("Search &backward"));

Findbutton = new Qpushbutton (tr ("&find"));

Findbutton->setdefault (TRUE);

Findbutton->setenabled (FALSE);

CloseButton = new Qpushbutton (tr ("Close"));

The TR () function is intended for translation into other languages. & used to identify shortcut keys.

Label->setbuddy (LineEdit); Set up partners. When the ALT+W fast key is pressed, the focus is transferred to the Lineedit control.

Connect (LineEdit, SIGNAL (textChanged (const QString &)),

This, SLOT (Enablefindbutton (const QString &)));

Connect (Findbutton, SIGNAL (clicked ()), this, SLOT (findclicked ()));

Connect (CloseButton, SIGNAL (clicked ()), this, SLOT (Close ()));

Private slot Enablefindbutton (const QString &) is called when the text of line editor is changed. Private findclicked () is called when the user taps the Find button. The close () slot inherits from Qwidget, and the default behavior is to hide the widget without deleting it. (This is similar to Java)

Since Qobject is the ancestor of Finddialog, we can omit the Qobject:: prefix in the Connect () call.

Qhboxlayout *topleftlayout = new Qhboxlayout;

Topleftlayout->addwidget (label);

Topleftlayout->addwidget (LineEdit);

Qvboxlayout * leftlayout = new Qvboxlayout;

Leftlayout->addlayout (topleftlayout);

Leftlayout->addwidget (Casecheckbox);

Leftlayout->addwidget (Backwardcheckbox);

Qvboxlayout *rightlayout = new Qvboxlayout;

Rightlayout->addwidget (Findbutton);

Rightlayout->addwidget (CloseButton);

Rightlayout->addstretch ();

Qhboxlayout *mainlayout = new Qhboxlayout;

Mainlayout->addlayout (leftlayout);

Mainlayout->addlayout (rightlayout);

SetLayout (mainlayout);

Use the layout manager. The layout manager can contain widgets or other layout managers. Using Qhboxlayout,qvboxlayout and qgridlayout with nesting allows you to generate very complex dialog boxes.

where Addstrech () is added below the spacer item (or stretch). This ensures that the top two buttons are at the bottom.

Setwindowtitle (tr ("Find"));

Setfixedheight (Sizehint (). height ());

}

The Qwidget::sizehint () function returns the actual size of a widget.

We created a lot of objects and did not write the related delete, just not needed, because when the parent object DESTROYED,QT automatically delete child objects.

Here's a look at dialog ' s slots:

void Finddialog::findclicked ()

{

QString text = Lineedit->text ();

Qt::casesensitivity cs = casecheckbox->ischecked ()?

qt::casesensitive:qt::caseinsensitive;

if (backwardcheckbox->ischecked ())

{

Emit findprevious (text, CS);

}

Else

{

Emit FindNext (text, CS);

}

}

void Finddialog::enablefindbutton (const QString &text)

{

Findbutton->setenabled (!text.isempty ());

}

The emit is the QT keyword, and the preprocessor turns it into C + + code.

Finally, main.cpp to test the Finddialog control.

#include <QApplication>

#include "Finddialog.h"

int main (int argc, char *argv[])

{

Qapplication app (argc, argv);

Finddialog *dialog = new Finddialog;

Dialog->show ();

return App.exec ();

}

Compilation issues:

Because Q_object macros are used, Qmake will contain special rules to run the MOC (Qt's Meta-object compiler).

Classes that use the Q_object macro must have an MOC run. Qmake will automatically add these rules to makefile.

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.