qt之旅-1纯手写Qt界面
1 新建一個empty qt project
2 配置pro文件
HEADERS += \Find.h QT += widgetsSOURCES += \Find.cpp \main.cpp
3 編寫對話框的類
代碼例如以下:
//Find.h #ifndef FIND_H #define FIND_H #include <QDialog> class QCheckBox; class QLabel; class QLineEdit; class QPushButton; class FindDialog : public QDialog {Q_OBJECT public:FindDialog(QWidget *parent = NULL); signals:void findNext(const QString &str,Qt::CaseSensitivity cs);void findPrevious(const QString &str,Qt::CaseSensitivity cs); private slots:void findClicled();void enableFindButton(const QString &text); private:QLabel* label;QLineEdit* lineEdit;QCheckBox* caseCheckBox;QCheckBox* backwardCheckBox;QPushButton* findButton;QPushButton* closeButton; }; #endif // FIND_H
//Find.cpp #include <QtGui> #include <QHBoxLayout> #include <QVBoxLayout> #include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QCheckBox> #include "Find.h"FindDialog::FindDialog(QWidget *parent) {label = new QLabel(tr("Find &what"));lineEdit = new QLineEdit;label->setBuddy(lineEdit);caseCheckBox = new QCheckBox(tr("Match &case"));backwardCheckBox = new QCheckBox(tr("Serach &backward"));findButton = new QPushButton(tr("&Find"));closeButton = new QPushButton(tr("&Close"));findButton->setDefault(true);findButton->setEnabled(false);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();QHBoxLayout* mainLayout = new QHBoxLayout;mainLayout->addLayout(leftLayout);mainLayout->addLayout(rightLayout);setLayout(mainLayout);setWindowTitle(tr("Find"));setFixedHeight(sizeHint().height()); }void FindDialog::findClicled() {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) { }
//main.cpp #include <QApplication> #include "Find.h"int main(int argc,char* argv[]) {QApplication app(argc,argv);FindDialog *dialog = new FindDialog;dialog->show();return app.exec(); }
3 觀察程序 類定義中的Q_OBEJECT
Q_OBEJECT是一個宏,對全部定義了信號和槽的類。在類定義開始處的Q_OBJECT宏是必須的。而且要直接或者間接的繼承QObject
4 signals
<1>signals也是一個宏,能夠在Qt的文件里看到Signals 就是 public,所以其前面不能加不論什么限定符.
<2>signals會被moc自己主動生成,所以一定不要在cpp文件里實現signals的函數
<3>signals的返回值僅僅能是void。
<4>signals中的函數原型必須和slots中的原型一致。例外的當slots中的參數比signals中少的時候。signal中的后面多余的參數會被忽略。
<5>signals 能夠有默認參數
5 slots
<1>slots 是普通的C++函數,前面能夠加限定符,public。private,protected,virtual。
<2>slots能夠有默認參數
<3> signals 和slots的機制非常像回調函數機制,就是用函數指針指向函數。
6 connect函數--信號和槽的連接
connec(sender,SIGNALE(xx),receiver,SLOTS(yy));
一個信號能夠連接多個槽;多個信號能夠連接到一個槽;一個信號能夠和信號連接;連接能夠被移除。
7 qt會在刪除父對象的時候會自己主動刪除全部的子對象,所以來寫析構函數來釋放新建的部件和布局是多余的。
8 布局
<1>Layout能夠加入widget;
<2>Layout能夠加入Layout
<3>widget能夠加入Layout
<4>QVBoxLayout。QHBoxLayout。分別表示縱向,橫向布局。
<5>程序終于一定要有個Layout來罩住全部的Layout。
9 main函數解析
轉載于:https://www.cnblogs.com/liguangsunls/p/6834823.html
總結
以上是生活随笔為你收集整理的qt之旅-1纯手写Qt界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 内存管理与系统架构设计
- 下一篇: 微信客户端<->腾讯微信服务器<->开发