##QT新手入门
QT简介:多平台C++图形界面应用程序框架
搭建开放环境
安装QT5.7或QT4
Hello QT实验
在main.cpp中简单地打印hello QT
#include<QApplication>
#include<QLabel>
int main(int argc,char*argv[])
{
QApplication app(argc,argv);
QLabel lable("<i><h2>Hi <font color=green>QT</font></i></h2>");
lable.show();
return app.exec();
}
app代表整个QT程序。
label是QLabel类的一个对象,.show()显示图形界面。
app.exec()产生事件循环,一直等待用户触发事件,然后进行处理,否则阻塞。
打印调试信息,需要包含头文件
用法:qDebug()<<“testing!”;
退出按钮实验
信号与槽-举例
两种格式-
connect(button4,SIGNAL(clicked(bool)),this,SLOT(close()));//QT4中常见格式
connect(button4,&QPushButton::pressed,this,&Widget::close);
格式2-QObject::connect(&a,SIGNAL(),&APP,SLOT());
解释:
QPushButton a(“退出按钮”);//声明按钮,实现按钮功能(信号和槽连接)
&a信号发射者
SIGNAL()信号
SIGNAL(clicked())
&APP信号接受者
SLOT()槽
SLOT(quit())//quit()槽函数
在刚刚的main函数中声明一个widget窗口部件pushbutton,然后连接信号与槽
Object::connect(button1,SIGNAL(click()),this,STOL(quit()));
知识点-命名规则
类名 大驼峰
对象名 变量 使用小驼峰
函数 一般使用小驼峰
窗体布置小实验
widget窗口,做滑动条,布局。参考
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2e5h1Bqq-1597877867826)(https://i.imgur.com/DVHpjUm.jpg)]
注:大多数类都是从QObject中继承,要使用信号与槽必须有QObject宏
头文件中声明:
QSpinBox *spinBox;//滑动值
QSlider *slider;//滑动条,滑竿
源文件中实现:
spinBox=new QSpinBox;//分配内存并调用构筑函数
slider=new QSlider(Qt::Horizontal);//水平放置滑竿
slider->setRange(0,150);//要滑竿和输入框设置同样大小才生效
spinBox->setRange(0,150);
QHBoxLayout *layout = new QHBoxLayout;//水平布局
layout->addWidget(spinBox);
layout->addWidget(slider);
etLayout(layout);
连接信号与槽
connect(spinBox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));
connect(slider,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));
UI界面介绍
简单加法实验
头文件声明指针和槽函数
private:
QLineEdit *line1;
QLineEdit *line2;
QLineEdit *line3;
QLabel *label;
QPushButton *button1;
private slots:
void add();//自定义STOL
在.cpp分配内存,初始化
label=new QLabel("+");
button1=new QPushButton("=");
line1=new QLineEdit;
line2=new QLineEdit;
line3=new QLineEdit;
QHBoxLayout *layout=new QHBoxLayout();
layout->addWidget(line1);
layout->addWidget(label);
layout->addWidget(line2);
layout->addWidget(button1);
layout->addWidget(line3);
setLayout(layout);
连接信号与槽
connect(button1,SIGNAL(clicked(bool)),this,SLOT(add()));
槽函数实现
void Widget::add(){
double a=line1->text().toDouble();
double b=line2->text().toDouble();
double c=a+b;
line3->setText(QString::number(c));
}
##新知识
参考网友原文https://www.kancloud.cn/kancloud/qt-study-road-2/99445
随机数小实验
在main.cpp中简单实现随机数
#include <QPushButton>
#include <QApplication>
#include <QtGlobal>
#include <QDebug>
#include <QDateTime>
void generateRandomNumber()
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
for(int i=0; i<10; i++)
{
int test =qrand()%10;
qDebug()<<test;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton w("quit");
w.show();
generateRandomNumber();
QObject::connect(&w,SIGNAL(clicked(bool)),&a,SLOT(quit()));
return a.exec();
}
信号与槽(多文件)
在newspaper中发出信号,reader接收,并打印收到的newspaper的名字
main.cpp:
#include "widget.h"
#include <QApplication>
#include"newspaper.h"
#include"reader.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Newspaper newspaper("报纸A");
Reader read;
QObject::connect(&newspaper,SIGNAL(newPaper(QString)),&read,SLOT(receiveNewspaper(QString)));
newspaper.send();
Widget w;
w.show();
return a.exec();
}
newspaper.h:
public:
explicit Newspaper(QObject *parent = 0);
Newspaper(const QString &name);
void send()const;
signals:
void newPaper(const QString &name)const;
private:
QString m_name;
newspaper.cpp:
#include "newspaper.h"
Newspaper::Newspaper(QObject *parent) : QObject(parent)
{}
Newspaper::Newspaper(const QString &name):
m_name(name)
{}
void Newspaper::send()const
{
emit newPaper(m_name);
}
reader.h:
public:
explicit Reader(QObject *parent = 0);
signals:
public slots:
void receiveNewspaper(const QString &name)const;
reader.cpp:
void Reader::receiveNewspaper(const QString &name)const
{
qDebug()<<"接收到:"<<name;
}