【qt学习006】Dialogs and MainWindows 小结
2020-11-27 08:40
标签:des style blog class code c 学习《c++GUI Programming with Qt 4》已有一段时间,非常享受这本书的阅读过程,内容简洁清晰,让人一目了然。 马上要学习更难的内容,所以先做个总结,然后再继续前进。 总结的形式尽量简洁,以代码为主,再将一些我认为重要的笔记作为注释添加在代码中。内容大多是摘抄自书本,但也有一些地方属于个人理解。 闲话少谈,下面列出代码: 【qt学习006】Dialogs and MainWindows 小结,搜素材,soscw.com 【qt学习006】Dialogs and MainWindows 小结 标签:des style blog class code c 原文地址:http://www.cnblogs.com/xy-kidult/p/3731923.html
// example1:#include "mainwindow.h"#include #include 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, container, 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. 开始执行,进入事件队列,由操作系统接管
}
// example2:#include #include #include #include #include int main(int
argc, char
*argv[])
{ QApplication app(argc, argv);
QWidget *window = new
QWidget; // the application‘s main window
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);
// 信号槽机制,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);
// 此处可以将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();
}
// dialog boxes // example 3// 学习有感,学一样东西,未必要从最新的地方入手,应该从资料最丰富的地方入手,如学qt4比qt5容易些,因为qt5刚发布,有很多新特性,新手容易混淆,并且遇见问题,因为资料少,无从下手解决。// main.cpp#include "mainwindow.h"#include #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 // 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) providesclass
QCheckBox;
class
QLabel;
class
QLineEdit;
class
QPushButton;
class
FindDialog: public
QDialog
{
// 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 #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());
}
// 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)这一章有点难度,比起之前的章节,这一章的知识点有些跳跃,需要写出一个完整的程序,包括布局、对象之间的继承、信号与槽的设置以及一些特殊的函数使用等等一些内容
// 另外章节的设置也有问题,这一章需要使用的类,在前两章由qt designer实现,无法直接在本章使用,需要自己去完善。// 如果学懂了 这一章,应该可以独立设计一个记事本。
文章标题:【qt学习006】Dialogs and MainWindows 小结
文章链接:http://soscw.com/index.php/essay/22866.html