日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

qt之旅-1纯手写Qt界面

發布時間:2023/11/30 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 qt之旅-1纯手写Qt界面 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過手寫qt代碼來認識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函數解析

#include <QApplication> #include "Find.h" int main(int argc,char* argv[]) {QApplication app(argc,argv); //app用來管理整個應用程序使用到的資源FindDialog *dialog = new FindDialog;dialog->show();return app.exec(); //將應用程序的控制權交給qt,程序會進入事件循環狀態。 }




轉載于:https://www.cnblogs.com/liguangsunls/p/6834823.html

總結

以上是生活随笔為你收集整理的qt之旅-1纯手写Qt界面的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。