青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

逛奔的蝸牛

我不聰明,但我會很努力

   ::  :: 新隨筆 ::  ::  :: 管理 ::

Subclassing QDialog

Our first example is a Find dialog written entirely in C++. We will implement the dialog as a class in its own right. By doing so, we make it an independent, self-contained component, with its own signals and slots.

 

Figure 2.1. The Find dialog

 


The source code is spread across two files: finddialog.h and finddialog.cpp. We will start withfinddialog.h.

1 #ifndef FINDDIALOG_H
2 #define FINDDIALOG_H
3 #include <QDialog>
4 class QCheckBox;
5 class QLabel;
6 class QLineEdit;
7 class QPushButton;

Lines 1 and 2 (and 27) protect the header file against multiple inclusions.

Line 3 includes the definition of QDialog, the base class for dialogs in Qt. QDialog inherits QWidget.

Lines 4 to 7 are forward declarations of the Qt classes that we will use to implement the dialog. A forward declaration tells the C++ compiler that a class exists, without giving all the detail that a class definition (usually located in a header file of its own) provides. We will say more about this shortly.

Next, we define FindDialog as a subclass of QDialog:

 8 class FindDialog : public QDialog
 9 {
10     Q_OBJECT
11 public:
12     FindDialog(QWidget *parent = 0);

The Q_OBJECT macro at the beginning of the class definition is necessary for all classes that define signals or slots.

The FindDialog constructor is typical of Qt widget classes. The parent parameter specifies the parent widget. The default is a null pointer, meaning that the dialog has no parent.

13 signals:
14     void findNext(const QString &str, Qt::CaseSensitivity cs);
15     void findPrevious(const QString &str, Qt::CaseSensitivity cs);

The signals section declares two signals that the dialog emits when the user clicks the Find button. If the Search backward option is enabled, the dialog emits findPrevious(); otherwise, it emits findNext().

The signals keyword is actually a macro. The C++ preprocessor converts it into standard C++ before the compiler sees it. Qt::CaseSensitivity is an enum type that can take the valuesQt::CaseSensitive and Qt::CaseInsensitive.

16 private slots:
17     void findClicked();
18     void enableFindButton(const QString &text);
19 private:
20     QLabel *label;
21     QLineEdit *lineEdit;
22     QCheckBox *caseCheckBox;
23     QCheckBox *backwardCheckBox;
24     QPushButton *findButton;
25     QPushButton *closeButton;
26 };
27 #endif

In the class's private section, we declare two slots. To implement the slots, we will need to access most of the dialog's child widgets, so we keep pointers to them as well. The slotskeyword is, like signals, a macro that expands into a construct that the C++ compiler can digest.

For the private variables, we used forward declarations of their classes. This was possible because they are all pointers and we don't access them in the header file, so the compiler doesn't need the full class definitions. We could have included the relevant header files (<QCheckBox><QLabel>, etc.), but using forward declarations when it is possible makes compiling somewhat faster.

We will now look at finddialog.cpp, which contains the implementation of the FindDialog class.

1  #include <QtGui>
2  #include "finddialog.h"

First, we include <QtGui>, a header file that contains the definition of Qt's GUI classes. Qt consists of several modules, each of which lives in its own library. The most important modules are QtCore, QtGui, QtNetwork, QtOpenGL, QtSqlQtSvg, and QtXml. The <QtGui>header file contains the definition of all the classes that are part of the QtCore and QtGuimodules. Including this header saves us the bother of including every class individually.

In filedialog.h, instead of including <QDialog> and using forward declarations for QCheckBox,QLabelQLineEdit, and QPushButton, we could simply have included <QtGui>. However, it is generally bad style to include such a big header file from another header file, especially in larger applications.

 3 FindDialog::FindDialog(QWidget *parent)
 4     : QDialog(parent)
 5 {
 6     label = new QLabel(tr("Find &what:"));
 7     lineEdit = new QLineEdit;
 8     label->setBuddy(lineEdit);
 9     caseCheckBox = new QCheckBox(tr("Match &case"));
10     backwardCheckBox = new QCheckBox(tr("Search &backward"));
11     findButton = new QPushButton(tr("&Find"));
12     findButton->setDefault(true);
13     findButton->setEnabled(false);
14     closeButton = new QPushButton(tr("Close"));

On line 4, we pass on the parent parameter to the base class constructor. Then we create the child widgets. The tr() function calls around the string literals mark them for translation to other languages. The function is declared in QObject and every subclass that contains theQ_OBJECT macro. It's a good habit to surround user-visible strings with TR(), even if you don't have immediate plans for translating your applications to other languages. Translating Qt applications is covered in Chapter 17.

In the string literals, we use ampersands ('&') to indicate shortcut keys. For example, line 11 creates a Find button, which the user can activate by pressing Alt+F on platforms that support shortcut keys. Ampersands can also be used to control focus: On line 6 we create a label with a shortcut key (Alt+W), and on line 8 we set the label's buddy to be the line editor. A buddy is a widget that accepts the focus when the label's shortcut key is pressed. So when the user presses Alt+W (the label's shortcut), the focus goes to the line editor (the label's buddy).

On line 12, we make the Find button the dialog's default button by calling setDefault(true). The default button is the button that is pressed when the user hits Enter. On line 13, we disable the Find button. When a widget is disabled, it is usually shown grayed out and will not respond to user interaction.

15     connect(lineEdit, SIGNAL(textChanged(const QString &)),
16             this, SLOT(enableFindButton(const QString &)));
17     connect(findButton, SIGNAL(clicked()),
18             this, SLOT(findClicked()));
19     connect(closeButton, SIGNAL(clicked()),
20             this, SLOT(close()));

The private slot enableFindButton(const QString &) is called whenever the text in the line editor changes. The private slot findClicked() is called when the user clicks the Find button. The dialog closes itself when the user clicks Close. The close() slot is inherited from QWidget, and its default behavior is to hide the widget from view (without deleting it). We will look at the code for the enableFindButton() and findClicked() slots later on.

Since QObject is one of FindDialog's ancestors, we can omit the QObject:: prefix in front of theconnect() calls.

21     QHBoxLayout *topLeftLayout = new QHBoxLayout;
22     topLeftLayout->addWidget(label);
23     topLeftLayout->addWidget(lineEdit);
24     QVBoxLayout *leftLayout = new QVBoxLayout;
25     leftLayout->addLayout(topLeftLayout);
26     leftLayout->addWidget(caseCheckBox);
27     leftLayout->addWidget(backwardCheckBox);
28     QVBoxLayout *rightLayout = new QVBoxLayout;
29     rightLayout->addWidget(findButton);
30     rightLayout->addWidget(closeButton);
31     rightLayout->addStretch();
32     QHBoxLayout *mainLayout = new QHBoxLayout;
33     mainLayout->addLayout(leftLayout);
34     mainLayout->addLayout(rightLayout);
35     setLayout(mainLayout);

Next, we lay out the child widgets using layout managers. Layouts can contain both widgets and other layouts. By nesting QHBoxLayouts, QVBoxLayouts, and QGridLayouts in various combinations, it is possible to build very sophisticated dialogs.

For the Find dialog, we use two QHBoxLayouts and two QVBoxLayouts, as shown in Figure 2.2. The outer layout is the main layout; it is installed on the FindDialog on line 35 and is responsible for the dialog's entire area. The other three layouts are sub-layouts. The little "spring" at the bottom right of Figure 2.2 is a spacer item (or "stretch"). It uses up the empty space below the Find and Close buttons, ensuring that these buttons occupy the top of their layout.

 

Figure 2.2. The Find dialog's layouts

 

 


One subtle aspect of the layout manager classes is that they are not widgets. Instead, they inherit QLayout, which in turn inherits QObject. In the figure, widgets are represented by solid outlines and layouts are represented by dashed outlines to highlight the difference between them. In a running application, layouts are invisible.

When the sub-layouts are added to the parent layout (lines 25, 33, and 34), the sub-layouts are automatically reparented. Then, when the main layout is installed on the dialog (line 35), it becomes a child of the dialog, and all the widgets in the layouts are reparented to become children of the dialog. The resulting parentchild hierarchy is depicted in Figure 2.3.

 

Figure 2.3. The Find dialog's parentchild relationships

 


36     setWindowTitle(tr("Find"));
37     setFixedHeight(sizeHint().height());
38 }

Finally, we set the title to be shown in the dialog's title bar and we set the window to have a fixed height, since there aren't any widgets in the dialog that can meaningfully occupy any extra vertical space. The QWidget::sizeHint() function returns a widget's "ideal" size.

This completes the review of FindDialog's constructor. Since we used new to create the dialog's widgets and layouts, it would seem that we need to write a destructor that calls delete on each of the widgets and layouts we created. But this isn't necessary, since Qt automatically deletes child objects when the parent is destroyed, and the child widgets and layouts are all descendants of the FindDialog.

Now we will look at the dialog's slots:

39 void FindDialog::findClicked()
40 {
41     QString text = lineEdit->text();
42     Qt::CaseSensitivity cs =
43             caseCheckBox->isChecked() ? Qt::CaseSensitive
44                                       : Qt::CaseInsensitive;
45     if (backwardCheckBox->isChecked()) {
46         emit findPrevious(text, cs);
47     } else {
48         emit findNext(text, cs);
49     }
50 }
51 void FindDialog::enableFindButton(const QString &text)
52 {
53     findButton->setEnabled(!text.isEmpty());
54 }

The findClicked() slot is called when the user clicks the Find button. It emits the findPrevious()or the findNext() signal, depending on the Search backward option. The emit keyword is specific to Qt; like other Qt extensions it is converted into standard C++ by the C++ preprocessor.

The enableFindButton() slot is called whenever the user changes the text in the line editor. It enables the button if there is some text in the editor, and disables it otherwise.

These two slots complete the dialog. We can now create a main.cpp file to test our FindDialogwidget:

1 #include <QApplication>
2 #include "finddialog.h"
3 int main(int argc, char *argv[])
4 {
5     QApplication app(argc, argv);
6     FindDialog *dialog = new FindDialog;
7     dialog->show();
8     return app.exec();
9 }

To compile the program, run qmake as usual. Since the FindDialog class definition contains theQ_OBJECT macro, the makefile generated by qmake will include special rules to run moc, Qt's meta-object compiler. (Qt's meta-object system is covered in the next section.)

For moc to work correctly, we must put the class definition in a header file, separate from the implementation file. The code generated by moc includes this header file and adds some C++ magic of its own.

Classes that use the Q_OBJECT macro must have moc run on them. This isn't a problem becauseqmake automatically adds the necessary rules to the makefile. But if you forget to regenerate your makefile using qmake and moc isn't run, the linker will complain that some functions are declared but not implemented. The messages can be fairly obscure. GCC produces warnings like this one:

     finddialog.o: In function 'FindDialog::tr(char const*, char const*)':
     /usr/lib/qt/src/corelib/global/qglobal.h:1430: undefined reference to
     'FindDialog::staticMetaObject'

Visual C++'s output starts like this:

     finddialog.obj : error LNK2001: unresolved external symbol
     "public:~virtual int __thiscall MyClass::qt_metacall(enum QMetaObject
     ::Call,int,void * *)"

If this ever happens to you, run qmake again to update the makefile, then rebuild the application.

Now run the program. If shortcut keys are shown on your platform, verify that the shortcut keys Alt+W, Alt+C, Alt+B, and Alt+F trigger the correct behavior. Press Tab to navigate through the widgets with the keyboard. The default tab order is the order in which the widgets were created. This can be changed using QWidget::setTabOrder().

Providing a sensible tab order and keyboard shortcuts ensures that users who don't want to (or cannot) use a mouse are able to make full use of the application. Full keyboard control is also appreciated by fast typists.

In Chapter 3, we will use the Find dialog inside a real application, and we will connect thefindPrevious() and findNext() signals to some slots.


posted on 2009-03-29 02:20 逛奔的蝸牛 閱讀(855) 評論(0)  編輯 收藏 引用 所屬分類: Qt
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            狠狠色狠色综合曰曰| 久久激情中文| 欧美一区二区三区免费观看| 亚洲人成在线观看| 亚洲精品中文字幕有码专区| 99riav1国产精品视频| 一区二区精品国产| 性欧美大战久久久久久久久| 久久福利资源站| 免费视频亚洲| 亚洲精品一区二区网址| 亚洲宅男天堂在线观看无病毒| 午夜激情久久久| 久久天堂国产精品| 欧美日韩亚洲国产一区| 国产欧美一区二区在线观看| 一区二区三区在线视频观看| 日韩亚洲视频| 久久久久久久久久久成人| 欧美aⅴ一区二区三区视频| 亚洲免费观看高清完整版在线观看熊 | 欧美午夜在线观看| 国产专区精品视频| 亚洲视频你懂的| 欧美大片一区二区| 午夜精品亚洲一区二区三区嫩草| 久久综合色天天久久综合图片| 欧美精品精品一区| 一区二区三区在线高清| 午夜精品成人在线| 91久久久久久久久| 性欧美大战久久久久久久免费观看| 欧美成人免费网站| 韩国成人福利片在线播放| 在线视频精品一区| 欧美国产1区2区| 欧美在线视频二区| 国产精品电影在线观看| 亚洲福利在线观看| 久久久久亚洲综合| 午夜激情综合网| 国产精品久久看| 一本色道久久综合亚洲二区三区| 久久久免费精品视频| 99国产一区| 欧美激情一区二区三区高清视频| 韩国av一区| 欧美一区1区三区3区公司| 夜色激情一区二区| 欧美激情一区在线观看| 在线欧美电影| 乱人伦精品视频在线观看| 午夜精品国产更新| 国产精品制服诱惑| 欧美专区中文字幕| 香蕉亚洲视频| 国产一区二区精品久久91| 欧美中文字幕在线播放| 亚洲专区在线| 国产老肥熟一区二区三区| 亚洲欧美日本伦理| 一区二区三区高清视频在线观看| 欧美激情无毛| 在线亚洲精品福利网址导航| 亚洲美女淫视频| 欧美日韩日本国产亚洲在线| 亚洲视频在线二区| 亚洲网站啪啪| 国产欧美日韩专区发布| 久久国产精品一区二区三区| 欧美亚洲一区二区在线| 国产在线观看一区| 麻豆成人在线| 麻豆精品传媒视频| 日韩亚洲不卡在线| 亚洲天堂成人在线视频| 国产精品亚洲美女av网站| 久久超碰97中文字幕| 欧美一区二区三区久久精品茉莉花| 国产啪精品视频| 欧美成人a视频| 欧美日韩国产高清| 欧美在线亚洲| 榴莲视频成人在线观看| 一片黄亚洲嫩模| 午夜精品免费在线| 亚洲全部视频| 亚洲欧美国产视频| 亚洲二区在线观看| 一级成人国产| 1000部国产精品成人观看| 亚洲国产精品成人精品| 国产精品视频久久| 嫩模写真一区二区三区三州| 欧美日韩一区二区视频在线观看| 欧美一区日本一区韩国一区| 久久综合久色欧美综合狠狠| 一区二区三区欧美视频| 午夜亚洲视频| 99pao成人国产永久免费视频| 亚洲一区二区在| 亚洲高清视频一区| 亚洲永久字幕| 日韩视频永久免费观看| 亚洲精品免费在线观看| 亚洲小说欧美另类婷婷| 欧美一区视频在线| 亚洲美女精品久久| 久久精品亚洲热| 亚洲欧美国产一区二区三区| 欧美插天视频在线播放| 久久九九热re6这里有精品| 欧美激情免费在线| 美女主播一区| 国产日韩欧美日韩大片| 日韩视频久久| 亚洲国产欧美一区二区三区久久| 亚洲视频综合在线| 日韩系列欧美系列| 久久在线免费观看| 久久久蜜桃精品| 国产农村妇女精品一二区| 亚洲最新中文字幕| 99热免费精品| 欧美激情免费在线| 欧美激情一区二区三区在线视频| 国产一区二区日韩精品| 亚洲一卡久久| 亚洲女同在线| 欧美日韩综合网| 亚洲精品看片| 日韩亚洲精品视频| 久久夜色撩人精品| 久久亚洲精品欧美| 国产一区二区三区丝袜| 亚洲影院污污.| 欧美一区二区精品| 国产欧美精品一区| 亚洲男人av电影| 午夜精品久久久久久久99樱桃| 欧美日韩免费在线| 亚洲婷婷综合色高清在线| 亚洲欧美国产视频| 国产精品视频999| 欧美一区二区在线| 蜜桃av综合| 亚洲精品在线三区| 欧美色另类天堂2015| 亚洲一区二区三区在线| 久久gogo国模裸体人体| 激情一区二区三区| 欧美成在线视频| 一区二区三区色| 久久精品国产91精品亚洲| 一区三区视频| 欧美激情第二页| 亚洲一区二区毛片| 久久在线视频在线| 亚洲免费成人| 国产精品www.| 久久久精品国产免大香伊| 亚洲高清不卡一区| 亚洲欧美在线观看| 精品1区2区3区4区| 欧美另类69精品久久久久9999| 亚洲精品美女91| 欧美一区二区三区视频| 精品av久久久久电影| 欧美激情中文字幕在线| 亚洲桃花岛网站| 免费亚洲一区| 亚洲无亚洲人成网站77777| 国产色婷婷国产综合在线理论片a| 亚洲午夜一区二区三区| 国产精品久久二区| 亚洲精品美女在线观看| 亚洲专区一区| 精品1区2区3区4区| 欧美日韩亚洲另类| 午夜久久久久久| 亚洲国产一区二区视频| 性色av一区二区三区| 亚洲国产精品热久久| 国产精品久久久久久久久免费樱桃| 欧美一级专区| 亚洲免费精彩视频| 欧美波霸影院| 欧美在线啊v| 中国成人黄色视屏| 在线看片欧美| 国产欧美在线视频| 欧美手机在线视频| 蜜桃久久av一区| 亚洲综合三区| 日韩午夜在线观看视频| 影音先锋欧美精品| 国内精品久久久久影院 日本资源| 欧美视频福利| 欧美日韩国产黄| 欧美激情综合网|