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

逛奔的蝸牛

我不聰明,但我會很努力

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

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>
            国产色视频一区| 亚洲经典自拍| 黄色一区二区三区四区| 日韩一区二区精品| 蜜桃视频一区| 久久久激情视频| 黄色成人免费观看| 久久久99爱| 欧美中在线观看| 激情欧美国产欧美| 老司机午夜精品| 久久久91精品国产一区二区三区| 国产精品免费一区豆花| 亚洲欧美一区二区视频| 亚洲视频免费在线观看| 国产精品私房写真福利视频| 欧美福利电影网| 永久91嫩草亚洲精品人人| 久久久视频精品| 久久久一区二区| 亚洲精品视频一区| 亚洲精品一区在线| 国产精品久久二区二区| 欧美一区二区在线| 欧美专区亚洲专区| 亚洲国产一区二区a毛片| 亚洲国产欧美一区二区三区丁香婷| 欧美成人一区二免费视频软件| 亚洲九九精品| 亚洲图中文字幕| 伊人精品视频| 日韩视频三区| 国产夜色精品一区二区av| 久久综合久久综合久久综合| 欧美成人午夜激情在线| 中日韩男男gay无套| 亚洲一二区在线| 一区精品在线| 99精品福利视频| 国产一区日韩二区欧美三区| 亚洲第一黄色| 国产精品美女久久久久久2018| 裸体丰满少妇做受久久99精品| 欧美激情一区在线观看| 欧美一区二区在线观看| 免费久久精品视频| 亚洲欧美精品一区| 老司机成人在线视频| 亚洲一区二区三区在线视频| 欧美在线不卡| 一二三区精品| 久久综合狠狠| 欧美在线不卡| 欧美激情在线有限公司| 欧美一区1区三区3区公司| 久久一综合视频| 午夜精品久久久久久久久| 老妇喷水一区二区三区| 欧美一区二区三区的| 欧美福利在线观看| 久热这里只精品99re8久| 国产精品女同互慰在线看| 亚洲黄色在线观看| 国内外成人免费激情在线视频| av成人激情| 在线观看成人av| 亚洲欧美日韩一区在线观看| 一本不卡影院| 欧美成人第一页| 久久永久免费| 国产一本一道久久香蕉| 亚洲午夜伦理| 亚洲欧美第一页| 欧美日韩免费高清| 亚洲茄子视频| 亚洲欧洲在线看| 久久这里只精品最新地址| 午夜一区二区三区在线观看| 欧美日韩一区精品| 亚洲精品视频在线| 亚洲毛片网站| 欧美激情在线| 中国成人亚色综合网站| 欧美精品久久久久久久免费观看| 狂野欧美一区| 伊人狠狠色j香婷婷综合| 久久精品国产2020观看福利| 久久精品1区| 国产一区二区三区在线观看视频| 午夜国产不卡在线观看视频| 欧美一区午夜精品| 国产麻豆综合| 欧美专区18| 欧美成人综合| 日韩亚洲精品电影| 欧美日韩一区二区在线 | 亚洲自拍三区| 亚洲欧美日韩一区二区三区在线观看 | 亚洲欧洲日韩女同| 久久人人爽人人| 欧美成人激情视频| 亚洲激情在线观看| 欧美精品久久99久久在免费线| 亚洲精品自在在线观看| 亚洲午夜视频| 国产乱码精品1区2区3区| 亚洲欧美日韩国产中文在线| 欧美一区二区三区在线播放| 国模私拍视频一区| 欧美11—12娇小xxxx| 日韩视频在线你懂得| 性做久久久久久久久| 激情成人av| 欧美精品尤物在线| 亚洲一区黄色| 蜜臀99久久精品久久久久久软件| av成人免费在线观看| 国产精品网站在线观看| 久久综合五月| 亚洲深夜福利网站| 狂野欧美一区| 亚洲一二三四久久| 激情久久久久久久| 欧美激情无毛| 欧美一乱一性一交一视频| 欧美激情乱人伦| 午夜精品视频在线| 欲色影视综合吧| 欧美日韩精品是欧美日韩精品| 亚洲欧美精品中文字幕在线| 鲁大师成人一区二区三区| 亚洲天堂成人| 韩国v欧美v日本v亚洲v| 欧美乱人伦中文字幕在线| 午夜精品福利电影| 亚洲电影免费观看高清| 欧美在线999| 99精品热6080yy久久| 国产日韩精品久久| 欧美日韩成人一区二区| 久久精品国产亚洲一区二区| 亚洲精品社区| 久久亚洲欧美| 亚洲欧美日韩国产一区| 亚洲精品在线观| 国产视频一区免费看| 欧美日韩高清在线一区| 久久久久久九九九九| 在线亚洲美日韩| 欧美一级视频| 在线观看视频亚洲| 国产乱码精品一区二区三区五月婷| 免费一级欧美片在线观看| 亚洲综合视频网| 99综合在线| 91久久久久久| 免费在线观看成人av| 久久久久国产免费免费| 午夜精品福利电影| 亚洲一区二区黄| 夜夜嗨av一区二区三区网页 | 另类专区欧美制服同性| 亚洲已满18点击进入久久| 亚洲国产婷婷| 亚洲国产精品视频| 极品日韩久久| 黄色在线一区| 一区二区三区在线观看欧美| 国产一区二区丝袜高跟鞋图片| 国产欧美在线视频| 国产精品免费看| 国产九九精品视频| 国产日韩精品综合网站| 国产日本欧美一区二区三区在线| 国产精品系列在线播放| 国产精品免费福利| 国产亚洲aⅴaaaaaa毛片| 国产永久精品大片wwwapp| 国产在线国偷精品产拍免费yy| 国产视频丨精品|在线观看| 国内一区二区在线视频观看| 在线观看的日韩av| 在线免费高清一区二区三区| 亚洲国产精品一区二区久| 亚洲欧洲三级| 一区二区不卡在线视频 午夜欧美不卡'| 亚洲精品国产欧美| 日韩视频免费在线| 亚洲视频一区二区免费在线观看| 一区二区电影免费在线观看| 亚洲欧美电影在线观看| 久久久久久国产精品mv| 欧美高清你懂得| 亚洲国产精品毛片| 日韩一级大片| 欧美一区二区视频97| 久久这里只精品最新地址| 欧美激情在线| 国产亚洲精品v| 亚洲国产精品激情在线观看|