通过手写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界面的更多相关文章

  1. springmvc 动态代理 JDK实现与模拟JDK纯手写实现。

    首先明白 动态代理和静态代理的区别: 静态代理:①持有被代理类的引用  ② 代理类一开始就被加载到内存中了(非常重要) 动态代理:JDK中的动态代理中的代理类是动态生成的.并且生成的动态代理类为$Pr ...

  2. 简易-五星评分-jQuery纯手写

    超级简单的评分功能,分为四个步骤轻松搞定: 第一步: 引入jquery文件:这里我用百度CDN的jquery: <script src="http://apps.bdimg.com/l ...

  3. vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件

    vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件/库 一提到移动端的下拉刷新上拉翻页,你可能就会想到iScroll插件,没错iScroll是一个高性能,资源 ...

  4. 超级简单的jQuery纯手写五星评分效果

    超级简单的评分功能,分为四个步骤轻松搞定: 第一步: 引入jquery文件:这里我用百度CDN的jquery: <script src="http://apps.bdimg.com/l ...

  5. 纯手写Myatis框架

    1.接口层-和数据库交互的方式 MyBatis和数据库的交互有两种方式: 使用传统的MyBatis提供的API: 使用Mapper接口: 2.使用Mapper接口 MyBatis 将配置文件中的每一个 ...

  6. SQL纯手写创建数据库到表内内容

    建表啥的只点点鼠标,太外行了,不如来看看我的纯手写,让表从无到有一系列:还有存储过程临时表,不间断的重排序: 一:建数据库 create Database Show on primary ( name ...

  7. 纯手写SpringMVC到SpringBoot框架项目实战

    引言 Spring Boot其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 通过这种方式,springboot ...

  8. 纯手写实现ajax分页功能

    前言 最近用到了这个功能,百度大半天,网上的不是有各种问题就是需要引入其他的插件,无奈,只能自己写,大致功能已经完成.前端页面用bootstrap做样式,分页功能用ajax实现,没用其他插件哦,只引入 ...

  9. 纯手写SpringMVC架构,用注解实现springmvc过程

    1.第一步,首先搭建如下架构,其中,annotation中放置自己编写的注解,主要包括service controller qualifier RequestMapping 第二步:完成对应的anno ...

随机推荐

  1. (5) openssl speed(测试算法性能)和openssl rand(生成随机数)

    1.1 openssl speed 测试加密算法的性能 支持的算法有: openssl speed [md2] [mdc2] [md5] [hmac] [sha1] [rmd160] [idea-cb ...

  2. EPT和VPID简介

    EPT(Extended Page Tables,扩展页表),属于Intel的第二代硬件虚拟化技术,它是针对内存管理单元(MMU)的虚拟化扩展.EPT降低了内存虚拟化的难度(与影子页表相比),也提升了 ...

  3. 如何用纯 CSS 创作一个冒着热气的咖啡杯

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/xjXxoz 可交互视频教程 此视 ...

  4. SolrCloud 概念

    原文链接 https://www.w3cschool.cn/solr_doc 当您的集合对于一个节点来说太大时,您可以通过创建多个分片将其分解并分段存储. 碎片是集合的逻辑分区,包含集合中的文档的子集 ...

  5. 【HIHOCODER 1601】 最大得分(01背包)

    描述 小Hi和小Ho在玩一个游戏.给定一个数组A=[A1, A2, ... AN],小Hi可以指定M个不同的值S1,S2, S3 ... SM,这样他的总得分是 ΣSi × count(Si).(co ...

  6. 大数据学习——面试用sql——累计报表

    create table t_access_times(username string,month string,salary int)row format delimited fields term ...

  7. python3--shelve 模块

    shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve d = shelve.open('shelve_t ...

  8. Android渐变GradientDrawable叠加组合环ring

     Android渐变GradientDrawable叠加组合环ring 写一个Android环形shape之间的叠加组合形成新图像的例子.代码: <?xml version="1. ...

  9. spring mvc dispatcherServlet

    1. 在web.xml中配置servlet对相应的url请求进行处理 <servlet> <servlet-name>springDispatcher</servlet- ...

  10. ibatis中的xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-/ ...