首页 > 代码库 > 【qt学习006】Dialogs and MainWindows 小结
【qt学习006】Dialogs and MainWindows 小结
学习《c++GUI Programming with Qt 4》已有一段时间,非常享受这本书的阅读过程,内容简洁清晰,让人一目了然。
马上要学习更难的内容,所以先做个总结,然后再继续前进。
总结的形式尽量简洁,以代码为主,再将一些我认为重要的笔记作为注释添加在代码中。内容大多是摘抄自书本,但也有一些地方属于个人理解。
闲话少谈,下面列出代码:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // example1: #include "mainwindow.h" #include <QApplication> #include <QLabel> int main( int argc, char *argv[]) { QApplication app(argc, argv); // The QApplication constructor requires argc and argv because Qt supports a few command-line arguments of its own. QLabel *label = new QLabel( "Hello Qt!" ); // Widget: control, <span style="background-color: rgb(255, 0, 0);">container</span>, a user interface in a user interface,容器的意思。 // Buttons, menus, scroll bars, and frames are all examples of widgets. Widgets can contain other widgets label->show(); return app.exec(); // passes control of the application on to Qt. // At this point, the program enters the event loop. 开始执行,进入事件队列,由操作系统接管 } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // example2: #include <QApplication> #include <QLabel> #include <QHBoxLayout> #include <QSlider> #include <QSpinBox> int main( int argc, char *argv[]) { QApplication app(argc, argv); QWidget *window = new QWidget; // the application‘s <span style="background-color: rgb(255, 0, 0);">main window</span> window->setWindowTitle( "Enter your age" ); // We could pass window to the QSpinBox and QSlider constructors, specifying that these widgets should have window as their parent QSpinBox * spinBox = new QSpinBox; QSlider *slider = new QSlider(Qt::Horizontal); spinBox->setRange(0, 130); slider->setRange(0, 130); <br> // 信号槽机制,qt核心本质之一 QObject::connect(spinBox, SIGNAL(valueChanged( int )), slider, SLOT(setValue( int ))); QObject::connect(slider, SIGNAL(valueChanged( int )), spinBox, SLOT(setValue( int ))); spinBox->setValue(35); // a layout manager,多控件时需要有布局管理器来管理各个控件的位置,qt的布局管理器类可以自动化布局,无需用户关心,写复杂的应用程序时可能会需要自己来布局。 QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(spinBox); layout->addWidget(slider); QVBoxLayout *layout1 = new QVBoxLayout; layout1->addWidget(spinBox); // automatically assigns reasonable positions and sizes to the widgets for which it is responsible, based on their needs layout1->addWidget(slider); <br> // 此处可以将Layout1设置为layout,体验一下不同的布局形式 window->setLayout(layout1); // an object that sets the size and position of the widgets that lie under its responsibility // installs the layout manager on the window window->show(); return app.exec(); } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | // dialog boxes // example 3 // 学习有感,学一样东西,未必要从最新的地方入手,应该从资料最丰富的地方入手,如学qt4比qt5容易些,因为qt5刚发布,有很多新特性,新手容易混淆,并且遇见问题,因为资料少,无从下手解决。 // main.cpp #include "mainwindow.h" #include <QApplication> #include "finddialog.h" int main( int argc, char *argv[]) { QApplication app(argc, argv); FindDialog *dialog = new FindDialog; dialog->show(); return app.exec(); } // finddialog.h #ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QDialog> // 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 class QCheckBox; class QLabel; class QLineEdit; class QPushButton; class FindDialog: public QDialog {<br> // qt核心本质之一 // qt macro, is necessary for all classes that define signals or slots Q_OBJECT public : // The default is a null pointer, meaning that the dialog has no parent. FindDialog(QWidget *parent = 0); signals: // qt macro 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 // finddialog.cpp // Including this header saves us the bother of including every class individually // contains the definition of Qt‘s GUI classes // bad style to include such a big header file from another header file, especially in larger applications. #include <QtGui> #include "finddialog.h" FindDialog::FindDialog(QWidget *parent) : QDialog(parent) // ??? we pass on the parent parameter to the base class constructor { label = new QLabel(tr( "Find &what:" )); // ??? ampersands (‘&‘) to indicate shortcut keys lineEdit = new QLineEdit; // 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). 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" )); 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())); 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(); // a spacer item (or "stretch"). QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(leftLayout); mainLayout->addLayout(rightLayout); setLayout(mainLayout); // the main layout is installed on the dialog setWindowTitle(tr( "Find" )); setFixedHeight(sizeHint().height()); } 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); //The emit keyword is specific to Qt } } void FindDialog::enableFindButton( const QString &text) { findButton->setEnabled(!text.isEmpty()); } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // example 4 // signals and slots // // connect(sender, SIGNAL(signal), receiver, SLOT(slot)); // sender and receiver are POINTERS to QObjects and where signal and slot are function // signatures WITHOUT PARAMETER names // To successfully connect a signal to a slot (or to another signal), they must have the // same parameter types in the same order: // connect(ftp, SIGNAL(rawCommandReply(int, const QString &)), // this, SLOT(processReply(int, const QString &))); // signals and slots mechanism don‘t limit to qt gui programming. // example 5 // 跳过了qt designer 几小节 // Qt‘s container widgets are widgets that contain other widgets, such as QFrame, // QGroupBox // QFrame can also be used on its own to simply draw lines and serves as the base class // for many other widget classes, including QToolBox and QLabel. // example 6: main windows // complete with menus, toolbars, status bar, and as many dialogs as the application requires // mainWdinows(Chapter3)这一章有点难度,比起之前的章节,这一章的知识点有些跳跃,需要写出一个完整的程序,包括布局、对象之间的继承、信号与槽的设置以及一些特殊的函数使用等等一些内容<br>// 另外章节的设置也有问题,这一章需要使用的类,在前两章由qt designer实现,无法直接在本章使用,需要自己去完善。 // 如果学懂了 这一章,应该可以独立设计一个记事本。 |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。