QT Learning Pathway (7): Create a dialog box (top)

Source: Internet
Author: User

First of all, in the C + + GUI programming with QT4, 2nd, this chapter, together with a number of subsequent chapters, completes a more complete program-a spreadsheet that mimics Excel. But it's a pretty big program, and the book does not give a complete source code, only segmented-I do not like this look, I want to see what I write is what, this is the most important, rather than slowly over the contents of a few chapters to see their own works. So I'm going to change the way that each chapter gives only simple knowledge, but each chapter is able to run something. All right, here we go!

It's basically about basics, and now we're really going to do something--a lookup dialog box. What the? What is a Lookup dialog box? Alas, first look at our final works!

Well, first create a new project, called Finddialog Bar! Well, of course it's the QT Gui application, and then the final step is to note that Base Dialog chooses Qdialog, not the default Qmainwindow, because we're going to learn to build dialog boxes! Name casually, but my name is Finddialog! Ganarate form is still not. Then finish the job.

Open Finddialog.h and start writing the header file.

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QtGui/QDialog>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
Q_OBJECT

public:
FindDialog(QWidget *parent = 0);
~FindDialog();
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};

#endif // FINDDIALOG_H

Everyone knows C + + Ah, so what #ifndef, #define和 #endif meaning and use will not repeat.

First, declare four classes that are used. This is a forward declaration, otherwise it is not compiled, because the compiler does not know if these classes exist. In short, the so-called forward declaration is to tell the compiler, I want to use these classes, and these classes exist, you do not worry that they do not exist problems!

Then is our finddialog, inherited from Qdialog.

Here's an important thing: Q_object. This is a macro. All classes that define a signal slot must declare the macro. As for why, we'll talk about it later.

Then is the public constructor and destructor declaration.

And then a signal: This is the key to QT-remember what you said earlier? QT extends the C + + language, so it has its own keywords--this is the definition of the signal, that is, the Finddialog has two public signals, it can emit these two signals at a specific time, and here, if the user clicks the Find button and selects search Backward, the FindPrevious () is issued, or the FindNext () is issued.

followed by the private slots: the definition, like the previous signal, is the definition of a private slot. That is, the Finddialog has two slots that can receive certain signals, but both slots are private.

For the purposes of slots, we need to access the Finddialog components, so we define the components as member variables for access. It is precisely because these components need to be defined that a forward declaration of their type is required. Because we're just using pointers, we don't involve the functions of these classes, so we don't need to include their headers--of course, you might want to introduce the header file directly, but then the compilation speed will be slower.

Well, the header file first said these, the next one again the source code!

Source: http://devbean.blog.51cto.com/448512/198971

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.