一、改变值的案例
QSlider 滑块类
void valueChanged(int val);
QSpinBox 微调框类(相当于下拉选择)
void setValue(int val);
槽函数如果不做连接,就相当于一个普通的函数
带参数的信号和槽,没有默认值时,需要在连接时指定参数的类型
#include<QApplication>
#include<QLable>
#include<QDialog>
#include<QLineEdit>
#include<QWidget>
#include<QPushButton>
#include<QSpinBox>
#include<QSlider>
#include<QObject>
int main(int argc,char** argv){
QApplication app(argc,argv);
/*这义组件指针*/
QDialog *parent;
QSlider *qsli;
QSpinBox *qspin;
/*赋值*/
parent = new QDialog();
parent -> resize(400,500);
qsli = new QSlider(parent);
qspin = new QSpinBox(parent);
/*调整组件位置*/
qsli->move(100,100);
qspin->move(200,200);
qspin->setValue(15);
parent->show();
//希望滑块对象影响微调框对象
QObject::connect(qsli,SIGNAL(valueChanged(int)),qspin,SLOT(setValue(int)));
//注意上面的信号函数带上了参数类型,必须这样写,否则编译不会通过
//希望微调框对象影响滑块对象
QObject::connect(qspin,SIGNAL(setValue(int)),qsli,SLOT(valueChanged(int)));
return app.exec();
}
二、加法器的实现
2.1 第一个版本的加法器
在main函数中
#include<QApplication>
#include<QLable>
#include<QDialog>
#include<QLineEdit>
#include<QPushButton>
#include<QLabel>
#include<QObject>
int main(int argc,char** argv){
QApplication app(argc,argv);
/*定义组件指针*/
QDialog *parent;
QLineEdit *add;
QLable *oper;
QLineEdit *added;
QPushButton *equ;
QLineEdit *res;
/*父窗口赋值和调整大小*/
parent = new QDialog();
parent->resize(650,200);
/*给组件指针赋值*/
add = new QLineEdit(parent);
oper = new QLabel("+",parent);
added = new QLineEdit(parent);
equ = new QPushButton("=",parent);
res = new QLineEdit(parent);
/*调整组件的位置*/
add->move(20,80);
oper->move(120,80);
added->move(320,80);
equ->move(350,80);
res->move(470,80);
parent->show();
return app.exec();
}
2.2 第二个版本的加法器
用C++方式(类)组织代码
1,让类继承父窗口类型
让当前类充当 父窗口类型 做到类的可视化
2,界面中的组件都作为这个类的成员变量
成员变量
3,类的头文件 和 实现文件分
4,如果类中有自定义的信号和槽,则需要在头文件中出现一个宏:Q_OBJECT ,否则连接无效
adder.h
#ifnedef ADDER_H
#define ADDER_H
#include<QLable>
#include<QDialog>
#include<QLineEdit>
#include<QWidget>
#include<QPushButton>
#include<QSpinBox>
#include<QSlider>
#include<QObject>
class Adder:public QDialog{
Q_OBJECT
private:
QLineEdit *add;
QLable *oper;
QLineEdit *added;
QPushButton *equ;
QLineEdit *res;
public:
Adder();//构造函数
/*扩展槽函数,自定义槽函数完成计算*/
public slots:
void getRes();
}
#endif
实现头文件, adder.cpp:
Adder::Adder(){
this->resize(650,200);
add = new QLineEdit(this);
oper = new QLabel("+",this);
added = new QLineEdit(this);
equ = new QPushButton("=",this);
res = new QLineEdit(this);
add->move(20,80);
oper->move(120,80);
added->move(320,80);
equ->move(350,80);
res->move(470,80);
//连接按钮到当前对象的槽函数
QObject::connect(equ,SIGNAL(clicked),this,SLOT(getRes()));
}
void Adder:getRes(){
//res->setText("9527");
/*
1,获取add、added的输入值
2,把获取到的值转成double
3,计算和,并显示在res
*/
}
测试文件,test.cpp:
#include<QApplication>
#include "adder.h"
int main(int argc,char** argv){
QApplication app(argc,argv);
Adder *adder = new Adder();
adder->show();
return app.exec();
}
三、信号和槽
1,信号和槽机制是qt编程的基础,可以使互不相干的对象绑定在一起。
2,槽可以和信号连接在一起,这样绑定之后,每当发射(触发)这个信号的时候,就会自动的调用这个槽
3,如果在类中要定义信号 槽必须声明Q_OBJECT
4,定义信号 public:signals
定义槽函数 public slots:
5,使用connect进行连接
connect(sender,SIGANL(signale),receiver,SLOT(slot));
6,连接可以移除
disconnect(lcd,SIGNAL(overflow()),this,SLOT(handleMathError));
注意:
- 一个信号可以连接多个槽
QObject::connect(sender,SIGANL(signa),receiver1,SLOT(slot1));
QObject::connect(sender,SIGANL(signa),receiver2,SLOT(slot2));
- 多个信号可以连接一个槽
- 一个信号可以与另外一个信号相连接,当发射第一个信号的时候,也会发射第二个信号
QObject::connect(a,SINGANL(siga()),b,SIGNAL(sigb));//信号->信号
QObject::connect(b,SINGANL(sigb()),c,SLOT(sigc));//信号->槽函数
a->b->c
- 当删除对象的时候,Qt也会自动的移除和这个对象相关的所有连接
四、是不是任意的信号 ,可以连接到任意的槽呢?不能,有限制
信号与槽连接的时候要注意的问题:
- 信号和槽要有相同的参数类型和参数顺序,参数的个数一般要求相同,但是信号的参数个数可以大于槽函数
- 连接带参的信号和槽时,在连接中需要出现参数的类型
如何让任意的信号连接到任意的槽?
QPushButton clicked();
QLabel setText(QString);
实现步骤:先让clicked和一个无参的自定义函数绑定,然后在自定义的槽函数中调用带参数的槽函数setText(参数值); (间接调用)
QTimer 定时器类 可以定时发出timeout信号
目标:让clicked信号与setText绑定到一起。(先自定义一个槽函数,在槽函数里通过访问自定义属性实现相关功能)
mytime.h
#ifndeif MYTIME_H
#define MYTIME_H
#include<QDialog>
#include<QLabel>
#include<QPushButton>
#include<QObject>
#include<QString>
class MyTime:public QDialog{
private:
QLabel *showtime;
QPushButton *gettime;
public :
MyTime();//构造函数
//槽函数,负责显示时间;
public slots:
void setTime();
//自定义信号函数,用来通知
void mySig(QString data);
}
#endif
mytime.cpp
#include "mytime.h"
MyTime::MyTime(){
this.resize(400,500);
showtime = new QLabel("curent time",this);
gettime = new PushButton("gettime",this);
showtime->move(150,50);
gettime->move(150,400);
//绑定按钮到当前对象的槽函数
connect(gettime,SIGNAL(clicked()),this,SLOT(setTime));
//自定义信号和setText(QString)绑定
//当点击按钮的时候,就会发射信号,一发射信号就会被setText捕获到
connect(this,SIGNAL(MySig(QString)),showtime,SLOT(setText(QString)));
}
#include<QTime>
//槽函数
void MyTime::setTime(){
QString timestr = QTime::currentTime().toString();
//showtime->setText(timestr);
//发射一个信号,信号应该带参
emit mySig(timestr);//通过emit发射信号出去
}
main.cpp
#include<QApplication>
#include "mytime.h"
int main(int argc,char** argv){
QApplication app(argc,argv);
Mytime my;
mt.show();
return app.exec();
}
定时器版本的自定义信号
myTime3.h
#ifndeif MYTIME_H
#define MYTIME_H
#include<QDialog>
#include<QLabel>
#include<QPushButton>
#include<QObject>
#include<QString>
#include<QTimer>
class MyTime:public QDialog{
private:
QLabel *showtime;
QPushButton *gettime;
public :
MyTime();//构造函数
//槽函数,负责显示时间;
public slots:
void setTime();
//自定义信号函数,用来通知
void mySig(QString data);
}
#endif
五、设计器的使用 designer
加速界面的开发,打开设计器:
tarena#designer
鼠标改变组件的位置和大小
方向键可以改变组件的位置
ctrl+方向键 可以微调组件的位置
最为精确的调整是在属性编辑器中调整
shift+方向键 可以调整组件的大小
ctrl+shift+方向键 微调组件大小
属性编辑器:可以改变组件的相关属性
对象查看器:查看对象之间的组织关系
对象名 和 对象的类型
六、如何使用ui文件
6.1 把ui文件转换成.h文件
uic .ui -o .h
6.2 定义自己的头文件 来使用生成的.h文件
6.3 写实现文件
创建界面 加载界面
访问界面数据使用ui即可
6.4写测试文件