qt之旅-1纯手写Qt界面
通过手写qt代码来认识qt程序的构成,以及特性。设计一个查找对话框。以下是设计过程
1 新建一个empty qt project
2 配置pro文件
HEADERS += \
Find.h
QT += widgets SOURCES += \
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,程序会进入事件循环状态。
}
qt之旅-1纯手写Qt界面的更多相关文章
- springmvc 动态代理 JDK实现与模拟JDK纯手写实现。
首先明白 动态代理和静态代理的区别: 静态代理:①持有被代理类的引用 ② 代理类一开始就被加载到内存中了(非常重要) 动态代理:JDK中的动态代理中的代理类是动态生成的.并且生成的动态代理类为$Pr ...
- 简易-五星评分-jQuery纯手写
超级简单的评分功能,分为四个步骤轻松搞定: 第一步: 引入jquery文件:这里我用百度CDN的jquery: <script src="http://apps.bdimg.com/l ...
- vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件
vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件/库 一提到移动端的下拉刷新上拉翻页,你可能就会想到iScroll插件,没错iScroll是一个高性能,资源 ...
- 超级简单的jQuery纯手写五星评分效果
超级简单的评分功能,分为四个步骤轻松搞定: 第一步: 引入jquery文件:这里我用百度CDN的jquery: <script src="http://apps.bdimg.com/l ...
- 纯手写Myatis框架
1.接口层-和数据库交互的方式 MyBatis和数据库的交互有两种方式: 使用传统的MyBatis提供的API: 使用Mapper接口: 2.使用Mapper接口 MyBatis 将配置文件中的每一个 ...
- SQL纯手写创建数据库到表内内容
建表啥的只点点鼠标,太外行了,不如来看看我的纯手写,让表从无到有一系列:还有存储过程临时表,不间断的重排序: 一:建数据库 create Database Show on primary ( name ...
- 纯手写SpringMVC到SpringBoot框架项目实战
引言 Spring Boot其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 通过这种方式,springboot ...
- 纯手写实现ajax分页功能
前言 最近用到了这个功能,百度大半天,网上的不是有各种问题就是需要引入其他的插件,无奈,只能自己写,大致功能已经完成.前端页面用bootstrap做样式,分页功能用ajax实现,没用其他插件哦,只引入 ...
- 纯手写SpringMVC架构,用注解实现springmvc过程
1.第一步,首先搭建如下架构,其中,annotation中放置自己编写的注解,主要包括service controller qualifier RequestMapping 第二步:完成对应的anno ...
随机推荐
- openjudge-4017 爬楼梯
总时间限制: 1000ms 内存限制: 65536kB 描述 树老师爬楼梯,他可以每次走1级或者2级,输入楼梯的级数,求不同的走法数 例如:楼梯一共有3级,他可以每次都走一级,或者第一次走一级,第二次 ...
- JavaScript基础对象---Map
一.创建Map对象 Map 对象保存键值对.任何值(对象或者原始值) 都可以作为一个键或一个值 1.构造函数 语法:new Map([iterable])参数: iterable 可 ...
- my97datepicker插件日期值改变事件 等同于input的onchang()时间
官网Demo地址http://www.my97.net/demo/index.htm <input type="text" class="Wdate" v ...
- 杭电 2553 N皇后问题 (dfs)
Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合 ...
- 【C#】【数据结构】005-栈:顺序栈
C#数据结构:顺序栈 1.自定义顺序栈结构: /// <summary> /// 顺序栈 /// </summary> /// <typeparam name=" ...
- python接口自动化-发xml格式post请求
前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...
- Idea中配置svn时报 Can't use Subversion command line client: svn.Errors found while svn working copies detection.
https://www.cnblogs.com/wqh17/p/6881483.html
- POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
- 基于神经网络的embeddding来构建推荐系统
在之前的博客中,我主要介绍了embedding用于处理类别特征的应用,其实,在学术界和工业界上,embedding的应用还有很多,比如在推荐系统中的应用.本篇博客就介绍了如何利用embedding来构 ...
- CSUOJ 1256 天朝的单行道
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1256 题目大意: 在另一个平行宇宙中,有一个神奇的国度名叫天朝.天朝一共有N个城 ...