Qt的安装
我是在kylin-v10的环境下进行下载的,与ubuntu是类似的
下载地址:https://download.qt.io/official_releases/qt/
可能需要赋予权限:chmod +x qt-opensource-linux-x64-5.14.2.run
打开安装包:./qt-opensource-linux-x64-5.9.2.run
Hello,world
1 2 3 4 5 6 7 8 9
| #include <QApplication> #include <QLabel>
int main(int argc, char *argv[]){ QApplication app(argc, argv); QLabel *label = new QLabel("Hello,world!"); label->show(); return app.exec(); }
|
成功啦,显示如下:

有意思的,可以在上述程序的基础上添加html标签:
1 2 3 4 5 6 7 8 9
| #include <QApplication> #include <QLabel>
int main(int argc, char *argv[]){ QApplication app(argc, argv); QLabel *label = new QLabel("<i>Hello</i>,<b>world!</b>"); label->show(); return app.exec(); }
|

建立连接
1 2 3 4 5 6 7 8 9 10
| #include <QApplication> #include <QPushButton>
int main(int argc, char *argv[]){ QApplication app(argc, argv); QPushButton *button = new QPushButton("Quit"); QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit())); button->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
| #include <QApplication> #include <QLabel> #include <QPushButton> #include <QHBoxLayout> int main(int argc, char *argv[]){ QApplication app(argc, argv);
QWidget *window = new QWidget;
QLabel *label = new QLabel("Hello,world!");
QPushButton *button = new QPushButton("Quit"); QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
QHBoxLayout * layout = new QHBoxLayout; layout->addWidget(label); layout->addWidget(button); window->setLayout(layout); 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
| #include <QApplication> #include <QLabel> #include <QPushButton> #include <QSpinBox> #include <QSlider> #include <QHBoxLayout> int main(int argc, char *argv[]){ QApplication app(argc, argv);
QWidget *window = new QWidget; QSlider *slider = new QSlider(Qt::Horizontal); QSpinBox *spinBox = new QSpinBox; spinBox->setRange(0,130); slider->setRange(0,130); QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int))); QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int))); spinBox->setValue(35); QHBoxLayout * layout = new QHBoxLayout; layout->addWidget(spinBox); layout->addWidget(slider); window->setLayout(layout); window->show();
return app.exec(); }
|

对connect的理解应该会更加深刻了。可以简单理解为两个组件之间的通信。前两个参数包含组件地址和SIGNAL;后两个参数包含另一个组件地址和SLOT
创建对话框
finddialog.h
文件
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
| #ifndef FINDDIALOG_H #define FINDDIALOG_H
#include <QDialog>
class QCheckBox; class QLabel; class QLineEdit; class QPushButton;
class FindDialog : public QDialog{ Q_OBJECT public: FindDialog(QWidge*parent = 0); signals: 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的实现:
注意,由于版本的问题,<QtGui>
改写为<QtWidgets>
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
| #include <QtWidgets> #include "finddialog.h"
FindDialog::FindDialog(QWidget *parent) : QDialog(parent){ label = new QLabel(tr("Find &what:")); lineEdit = new QLineEdit; 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(closed()));
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();
QHBoxLayout * mainLayout = new QHBoxLayout; mainLayout->addLayout(leftLayout); mainLayout->addLayout(rightLayout); setLayout(mainLayout);
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); } }
void FindDialog::enableFindButton(const QString &text){ findButton->setEnabled(!text.isEmpty()); }
|
main函数:
1 2 3 4 5 6 7 8 9 10 11
| #include <QApplication>
#include "finddialog.h"
int main(int argc, char *argv[]){ QApplication app(argc, argv); FindDialog *dialog = new FindDialog; dialog->show(); return app.exec(); }
|

父子关系:

最终实现:
关于信号与槽
- 一个信号可以连接多个槽
- 多个信号可以连接同一个槽
- 一个信号可以与另外一个信号相连接
- 连接可以被移除
- 信号与槽的参数必须具有相同的顺序与类型。(信号的参数若多于槽,则多余参数会被忽略)
参考资料
- 《C++ GUI Qt 4 编程(第二版)》《C++ GUI programming with
Qt4》电子工业出版社