First of all, in C ++ GUI Programming with Qt4, 2nd, this chapter, together with several subsequent chapters, completed a relatively complete Program-a spreadsheet that imitates Excel. However, this program is big, and the complete source code is not provided in the book. It is just segmented-I don't like it. I want to see what I wrote, this is the most important thing, rather than reading a few chapters to see your work. Therefore, I intend to use another method. Each chapter only provides simple knowledge, but each chapter can run something. Okay. Now, let's get started! I used to talk about some basic knowledge. Now let's make a real thing-a Search dialog box. What? What is the Search dialog box? Alas, let's take a look at our final work! 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1T015FL-0.png "/> Well, first create a project, called FindDialog! Well, of course, we should still use the Qt Gui Application. Next, we should note that we should select QDialog for Base Dialog instead of the default QMainWindow, because we need to learn how to create a Dialog box! Just give me the name, but I'm finddialog! Ganarate form is not expected. Then Finish. Open finddialog. h and write the header file. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> # ifndef FINDDIALOG_H
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> # define FINDDIALOG_H
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> # include <QtGui/QDialog>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> class QCheckBox;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> class QLabel;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> class QLineEdit;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> class QPushButton;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> class FindDialog: public QDialog
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> Q_OBJECT
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> public:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> FindDialog (QWidget * parent = 0 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> ~ FindDialog ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> signals:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> void findNext (const QString & str, Qt: casesensiti1_cs );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> void findPrevious (const QString & str, Qt: casesensiti1_cs );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> private slots:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> void findClicked ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> void enableFindButton (const QString & text );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> private:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> QLabel * label;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> QLineEdit * lineEdit;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> QCheckBox * caseCheckBox;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> QCheckBox * backwardCheckBox;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> QPushButton * findButton;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> QPushButton * closeButton;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1T0154A3-1.gif"/> # endif // FINDDIALOG_H everyone knows C ++, So what # ifndef, # define and # the meaning and purpose of endif will not be described in detail. First, declare four classes used. Here we do the Forward Declaration, otherwise the compilation will fail, because the compiler does not know whether these classes exist. In short, the so-called Forward Declaration tells the compiler that I want to use these classes, and these classes exist, so you don't have to worry about the problems they don't exist! Then our FindDialog inherits from QDialog. The following is an important thing: Q_OBJECT. This is a macro. This macro must be declared for all classes that define the signal slot. As for why, let's talk about it later. Then the public constructor and destructor declaration. Then there is a signal:, this is the key word of Qt-Remember what I said before? Qt extends the C ++ language, so it has its own keyword-this is the definition of the signal, that is, FindDialog has two public signals, it can send these two signals at a specific time. Here, if you click the Find button and select Search backward, findPrevious () will be sent (), otherwise, findNext () is issued (). The next step is the definition of private slots: Like the previous signal, which is the definition of private slots. That is to say, the FindDialog has two slots that can receive some signals, but both are private. To define slots, We need to access the FindDialog component. Therefore, we define the component as a member variable for access. It is precisely because these components need to be defined that they need to be declared in the forward direction. Because we only use pointers and do not involve functions of these classes, we do not need to include their header files-of course, you can also directly introduce header files, however, the Compilation speed will be slower. Okay, let's talk about this first in the header file, and next we will talk about the source code! Take a break!
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/198971