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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392894.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392894.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392894.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【隨筆】寫在2014年的第一天

想想好像就在不久前還和大家異常興奮地討論著世界末日的事&#xff0c;結果一晃也是一年前的事了。大四這一年&#xff0c;或者說整個2013年都是場搖擺不定的戲劇&#xff0c;去過的地方比前三年加起來還多的多&#xff0c;有時候也會恍惚地不知道自己現在在哪。簡單記幾筆&…

設計沖刺下載_如何運行成功的設計沖刺

設計沖刺下載by George Krasadakis通過喬治克拉薩達基斯(George Krasadakis) Design Sprints can generate remarkable output for your company — such as a backlog of impactful ideas, functional prototypes, learning and key insights from customers along with real…

leetcode 18. 四數之和(雙指針)

給定一個包含 n 個整數的數組 nums 和一個目標值 target&#xff0c;判斷 nums 中是否存在四個元素 a&#xff0c;b&#xff0c;c 和 d &#xff0c;使得 a b c d 的值與 target 相等&#xff1f;找出所有滿足條件且不重復的四元組。 注意&#xff1a; 答案中不可以包含重…

WPF:從WPF Diagram Designer Part 4學習分組、對齊、排序、序列化和常用功能

在前面三篇文章中我們介紹了如何給圖形設計器增加移動、選擇、改變大小及面板、縮略圖、框線選擇和工具箱和連接等功能&#xff0c;本篇是這個圖形設計器系列的最后一篇&#xff0c;將和大家一起來學習一下如何給圖形設計器增加分組、對齊、排序、序列化等功能。 WPF Diagram D…

win7如何看計算機用戶名和密碼怎么辦,win7系統電腦查看共享文件夾時不顯示用戶名和密碼輸入窗口的解決方法...

win7系統使用久了&#xff0c;好多網友反饋說win7系統電腦查看共享文件夾時不顯示用戶名和密碼輸入窗口的問題&#xff0c;非常不方便。有什么辦法可以永久解決win7系統電腦查看共享文件夾時不顯示用戶名和密碼輸入窗口的問題&#xff0c;面對win7系統電腦查看共享文件夾時不顯…

ASP.NET Core跨域設置

項目中經常會遇到跨域問題&#xff0c;解決方法&#xff1a; 在appsettings.json 文件中添加json項 {"Logging": {"LogLevel": {"Default": "Warning"}},"AllowedHosts": "*","AppCores": "https…

微信客戶端<->騰訊微信服務器<->開發者服務器

出自 http://blog.csdn.net/hanjingjava/article/details/41653113 首先&#xff0c;通過Token驗證&#xff0c;將公眾號接入開發者服務器&#xff0c;這樣客戶端發給公眾號的信息會被轉發給開發者服務器&#xff1b; 第二&#xff0c;組裝微信特定消息格式&#xff0c;返回給用…

idea提高調試超時_如何提高您的調試技能

idea提高調試超時by Nick Karnik尼克卡尼克(Nick Karnik) 如何提高您的調試技能 (How to Improve Your Debugging Skills) All of us write code that breaks at some point. That is part of the development process. When you run into an error, you may feel that you do…

leetcode 834. 樹中距離之和(dp)

給定一個無向、連通的樹。樹中有 N 個標記為 0...N-1 的節點以及 N-1 條邊 。第 i 條邊連接節點 edges[i][0] 和 edges[i][1] 。返回一個表示節點 i 與其他所有節點距離之和的列表 ans。示例 1:輸入: N 6, edges [[0,1],[0,2],[2,3],[2,4],[2,5]] 輸出: [8,12,6,10,10,10] 解…

CSS設計指南(讀書筆記 - 背景)

本文轉自william_xu 51CTO博客&#xff0c;原文鏈接&#xff1a;http://blog.51cto.com/williamx/1140006&#xff0c;如需轉載請自行聯系原作者

在計算機網絡中 帶寬是什么,在計算機網絡中,“帶寬”用____表示。

答案查看答案解析:【解析題】計算機的發展經歷了4個時代&#xff0c;各個時代劃分的原則是根據()。【解析題】計算機網絡的最主要的功能是______。【解析題】馮.諾依曼提出的計算機工作原理為____。【解析題】計算機的通用性使其可以求解不同的算術和邏輯問題&#xff0c;這主要…

如何在iOS上運行React Native應用

by Soujanya PS通過Soujanya PS 如何在iOS上運行React Native應用 (How to run a React Native app on iOS) I recently started to develop a React-Native app on iOS. This was my first foray into native app development. I was surprised by the ease and level of abs…

導出excel 后 頁面按鈕失效(頁面假死)

在 page_load 里加上如下代碼&#xff1a;string beforeSubmitJS "\nvar exportRequested false; \n"; beforeSubmitJS "var beforeFormSubmitFunction theForm.onsubmit;\n"; beforeSubmitJS "theForm.onsubmit function(){ \n"; …

Mysql分組查詢group by語句詳解

(1) group by的含義:將查詢結果按照1個或多個字段進行分組&#xff0c;字段值相同的為一組(2) group by可用于單個字段分組&#xff0c;也可用于多個字段分組 select * from employee; --------------------------------------------- | num | d_id | name | age | sex | homea…

leetcode 75. 顏色分類(雙指針)

給定一個包含紅色、白色和藍色&#xff0c;一共 n 個元素的數組&#xff0c;原地對它們進行排序&#xff0c;使得相同顏色的元素相鄰&#xff0c;并按照紅色、白色、藍色順序排列。 此題中&#xff0c;我們使用整數 0、 1 和 2 分別表示紅色、白色和藍色。 注意: 不能使用代碼…

火車頭如何才能設置發布的時候,如果是有html代碼就直接的轉換掉,互聯網上笑話抽取及排重---火車頭采集器的使用和MD5算法的應用...

10011311341 呂濤、10011311356李紅目的&#xff1a;通過熟悉使用火車頭采集器&#xff0c;在網絡上采取3萬條笑話并進行排重&#xff0c;以此來熟悉web文本挖掘的一些知識。過程&#xff1a;本次學習&#xff0c;主要分成兩個部分。第一部分是笑話文本的采集&#xff0c;第二部…

Tcp_wrapper

在Linux進程分為&#xff1a;獨立進程和非獨立進程非獨立進程&#xff1a;是依賴于超級守護進程的進程&#xff0c; 且受Xinetd 管理&#xff0c;并在啟動服務時 必須啟動例子&#xff1a;#chkconfig –level 2345 telnetd on關與chkconfig 的命令&#xff1a;#chkconfig –lis…

angular 動畫_如何在Angular 6中使用動畫

angular 動畫介紹 (Introduction) Animation is defined as the transition from an initial state to a final state. It is an integral part of any modern web application. Animation not only helps us create a great UI but it also makes the application interesting…

win10上面安裝win7的虛擬機怎么相互ping通

最近干了一些很蛋疼的事&#xff0c;這些都是自己踩過的坑&#xff0c;記錄下來方便自己以后查閱 首先我的目的就是為了在自己的PC機上面部署一個SVN服務器&#xff0c;然后安裝一個客戶端&#xff0c;自己寫的軟件就可以定期入庫&#xff0c;做好自己的版本控制&#xff0c;但…

新東方面試知識點記錄

3.spring mvc 怎么接受http post 方式提交過來的xml數據&#xff1f;servlet中怎么接受&#xff1f; RequestMapping(value"/jsonPrase", headers {"content-typeapplication/json","content-typeapplication/xml"}) ResponseBody …