回顾:
布局:
绝对位置法:
手工布局:QLayout
可视化布局:设计器
VB MFC JAVA C# cocos

QT信号和槽

----------------------------------
1.信号与槽 Signals & Slots
信号和槽是一种高级接口(函数),是QT自定义的一种通信机制,独立于标准C++
应用于对象之间的通信,是QT的核心特性,也是QT区别于其它工具包的重要地方。

所有的QObject或其子类(如QWidget)派生的类都包含信号和槽。

Q_OBJECT//这是一个宏 如果需要进行对象之间的通信,必须有这个宏的支持。

信号:当对象改变其状态时,信号就由该对象发射(emit)出去。
当一个信号被发射时,与其有关联的槽将立刻被执行,就像函数调用一样。

槽:槽函数。是普通的C++成员函数,可以被正常调用,和普通成员函数唯一的区别就是可以和信号连接
也有访问权限。

信号和槽关联:
connect(
//信号发出者,信号,信号接收者,槽函数
sender,SIGNAL(signal),receiver,SLOT(slot)
)
connect(button,SIGNAL(clicked()),this,SLOT(close()));

每个QT对象都包含预定的信号和槽。

自定义信号和槽:
自定义的槽:
public slots:
//槽函数声明
自定义信号:
signals:
//信号声明(信号只需要声明,不需要实现)

发射信号:emit 信号;

注:
1.槽不能与槽相联
2.信号与槽的参数要一致(类型,个数,顺序),信号的参数可以比槽的参数多,不能少。
3.信号与槽的参数只需要写类型就可以了,不需要写形参。

案例:初步实现登录功能(即用户名和密码已经确定)

----------------------
2.QString类:
QString类提供了多个函数来操作字符串
编辑操作:
append()/prepend()
replace()
insert()
remove()
isEmpty()

查询操作:
right(),left(),mid(),indexOf()
at(),contains(),count(),startWidth(),endWith()
> < ...
转换操作:
toInt(),toDouble()
number()//静态函数
toLower()/toUpper()
arg()//
QString -> char *
//toUtf8().data()

cal

代码1;

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

public slots://自定义槽函数
void mySlots(bool);
void emitSignalSlot();

signals:
void mySignal();

private:
Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//QPushButton *btn = new QPushButton;
ui->pushButton->setText("关闭");
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
ui->dial,SLOT(setValue(int)));

ui->horizontalSlider->setMaximum(200);
ui->dial->setMaximum(200);

connect(ui->btn,SIGNAL(clicked(bool)),this,SLOT(mySlots(bool)));
// connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(emitSignalSlot()));
// connect(this,SIGNAL(mySignal()),this,SLOT(close()));
}

Widget::~Widget()
{
delete ui;
}

void Widget::mySlots(bool ok)//自定义的槽函数
{

ui->btn->setCheckable(true);//设置按钮可选
bool ok1 = ui->btn->isCheckable();//获取按钮是否可选定
//qDebug() << ok1;
if(ok)
{
ui->btn->setText("打开");
}
else
{
ui->btn->setText("关闭");
}

}

void Widget::emitSignalSlot()//发射信号的槽函数
{
qDebug("emitSignalslot()");
emit mySignal();
}

代码2

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
   // w.show();

    return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private:
Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QString str = "hello qt";
str.append("7-20");//追加
str.replace("qt","c++");//替换
str.insert(2,"A");//插入,下标从0开始
//remove
bool ok = str.isEmpty();//判断字符串是否为空

//qDebug() << str;//str = heAllo c++7-20
//------------------------------
//qDebug() << str.right(3);//str的右边3个字符
//qDebug() << str.mid(5,3);//从第5个位置取3个字符
//qDebug() << str.indexOf("++");//获得指定字符串在str中出现的索引位置
//qDebug() << str.at(0);//取第i个位置的值
//qDebug() << str.contains("20");//查询str中是否包含指定的字符串
//qDebug() << str.contains("30");
//qDebug() <<str.count("l");//指定字符串出现的次数
//qDebug() << str.startsWith("h");
//-----------------------------------
QString s = "123.45";
int a;
//a = s.toInt();//把字符串转为整数
double d = s.toDouble();//转为双精度数
qDebug() << s;
qDebug() << d;
QString sd =
QString::number(d);//把数字转为字符串
qDebug() << sd;
//用户名为XXX 密码为XXX
QString name = "zhangfei";
QString pwd = "123";
str = QString("用户名为%1 密码为%2").arg(name).arg(pwd);
qDebug() << str;

char *p = "hello";
QString ss = "world";
// ss.toStdString().data();//把QString转换为char*
// ss.toUtf8().data();
// ss.toLatin1().data();
// ss.toLocal8Bit().data();

}

Widget::~Widget()
{
delete ui;
}

代码三

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void calcSlot();

private slots:
void on_pushButton_clicked();

private:
Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->lineEdit_3->setReadOnly(true);//设置文本框为只读
// connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(calcSlot()));
}

Widget::~Widget()
{
delete ui;
}

void Widget::calcSlot()//slot
{
//1.获取两个加数
int n1 = ui->lineEdit->text().toInt();
int n2 = ui->lineEdit_2->text().toInt();
//2.两数相加
int res = n1 + n2;
//3.把结果显示出来
ui->lineEdit_3->setText(QString::number(res));
}

void Widget::on_pushButton_clicked()
{
calcSlot();
}

代码四

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "mainform.h"
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void loginSlot();

private:
Ui::Widget *ui;
mainForm *mf;
};

#endif // WIDGET_H

mainform.h

#ifndef MAINFORM_H
#define MAINFORM_H

#include <QWidget>

namespace Ui {
class mainForm;
}

class mainForm : public QWidget
{
    Q_OBJECT

public:
    explicit mainForm(QWidget *parent = 0);
    ~mainForm();

private:
    Ui::mainForm *ui;
};

#endif // MAINFORM_H

mainfrom.cpp

#include "mainform.h"
#include "ui_mainform.h"

mainForm::mainForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::mainForm)
{
ui->setupUi(this);
}

mainForm::~mainForm()
{
delete ui;
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(loginSlot()));
mf = new mainForm;
}

void Widget::loginSlot()
{
//获取用户输入的用户名和密码
QString name = ui->lineEdit->text();
QString pwd = ui->lineEdit_2->text();
qDebug() << name;
qDebug() << pwd;
//2.判断用户名和密码是否正确
if(name == "zhangfei" && pwd == "123")
{
qDebug() << "登录成功!";

//1.把自己关闭或隐藏
//this->close();
this->hide();
//2.显示主窗体(登录成功后显示的窗体)
mf->show();
}
else
{
qDebug() << "用户名或密码错误";
}
}

Widget::~Widget()
{
delete ui;
}

QT第三天学习的更多相关文章

  1. Qt Installer Framework的学习(三)

    Qt Installer Framework的学习(三) Qt Installer Framework的样例中.通常是这种:config目录一般放了一个config.xml文件,包括的是安装配置xml ...

  2. Qt学习 之 多线程程序设计(QT通过三种形式提供了对线程的支持)

    QT通过三种形式提供了对线程的支持.它们分别是, 一.平台无关的线程类 二.线程安全的事件投递 三.跨线程的信号-槽连接. 这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线 ...

  3. QT的三种协议说明

    关于Qt的三种协议以及是否收费,有以下引文: 引文一: 最近一直在学习 Qt.Qt 有两个许可证:LGPL 和商业协议.这两个协议在现在的 Qt 版本中的代码是完全一致的(潜在含义是,Qt 的早期版本 ...

  4. Qt Creator 源码学习笔记03,大型项目如何管理工程

    阅读本文大概需要 6 分钟 一个项目随着功能开发越来越多,项目必然越来越大,工程管理成本也越来越高,后期维护成本更高.如何更好的组织管理工程,是非常重要的 今天我们来学习下 Qt Creator 是如 ...

  5. Qt Creator 源码学习笔记04,多插件实现原理分析

    阅读本文大概需要 8 分钟 插件听上去很高大上,实际上就是一个个动态库,动态库在不同平台下后缀名不一样,比如在 Windows下以.dll结尾,Linux 下以.so结尾 开发插件其实就是开发一个动态 ...

  6. 20145213《Java程序设计》第三周学习总结

    20145213<Java程序设计>第三周学习总结 教材学习内容总结 正所谓距离产生美,上周我还倾心于Java表面的基础语法.其简单的流程结构,屈指可数的基本类型分类,早已烂熟于心的运算符 ...

  7. 20145304 Java第三周学习报告

    20145304 <Java程序设计>第三周学习总结 教材学习内容总结 1.定义类: 类定义时使用class关键词,建立实例要使用new关键词. 代码如下: /*定义类 书上例子 衣服的型 ...

  8. 20145330《Java程序设计》第三周学习总结

    20145330 <Java程序设计>第三周学习总结 第三周知识的难度已经逐步上升,并且一周学习两章学习压力也逐渐加大,需要更高效率的来完成学习内容,合理安排时间. 类与对象 对象(Obj ...

  9. 20145337《Java程序设计》第三周学习总结

    20145337 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象 类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象是类的实例.我觉得在视频中对类与对象关系的描 ...

随机推荐

  1. Quartz总结(四):动态修改定时器二

    前文:http://www.cnblogs.com/LiuChunfu/p/5598806.html 提到了一种动态修改定时器的方法, 其本质就是在job方法中注入Schedular的对象,从Sche ...

  2. 路过Haxe

    刚才在看Nape的时候,看到Haxe的代码,意外的感觉到亲切. 因为之前写过as2代码,最近学习了python,所以对haxe看起来很亲切,于是路过一下写了个HelloWorld. 另外,估计很长时间 ...

  3. openstack controller ha测试环境搭建记录(十二)——配置neutron(计算节点)

    在计算节点配置内核参数:vi /etc/sysctl.confnet.ipv4.conf.all.rp_filter=0net.ipv4.conf.default.rp_filter=0 在计算节点使 ...

  4. HAProxy 7层 负载均衡

    系统 CentOS 5.8 x64 wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.26.tar.gz cd haproxy-1.3.2 ...

  5. ASP.NET AJAX注册命名空间

    AJAX库支持命名空间的概念. 类可以分组到不同的命名空间.AJAX库会自动创建在此层次中不存在的任何级别的命名空间. <%@ Page Language="C#" Auto ...

  6. Javascript Fromdata 与jQuery 实现Ajax文件上传以及文件的删除

    前端HTML代码: <!DOCTYPE html> <html> <head> <title>ajax</title> <script ...

  7. C# DateTime简单的定时器用法

    DateTime是C#中的时间类,有公共索引器Now可以获取当前时间. 如果制作简单定时器的话,可以这样: 初始化:DateTime lasttime=DateTime.Now; TimeSpan t ...

  8. Eclipse中GIT插件更新工程到之前版本

    因为之前好多次因为对项目文件删除后,发现删除的文件里有些功能模块还是需要的,所以需要恢复到之前的版本.但是一直不知道怎么操作才能恢复到之前版本,索性就直接把工程删了,重新导入,但是这太暴力了,所以看了 ...

  9. POJ 3362 Protecting the Flowers

    这题和金华区域赛A题(HDU 4442)是一样的做法. 对两个奶牛进行分析,选择两个奶牛总花费少的方式排序. bool cmp(const X&a,const X&b){ return ...

  10. github上forck一个分支之后,如何和主分支同步

    github forck一个分之后,如果过一段时间就会和主分支的差异比较大. 这样提交pr的时候 就会冲突,这个时候我们就需要和主分支同步代码 git remote add upstream git@ ...