1.代码的巩固
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//QPushButton btn1(this); //在栈区申请一个按钮
//栈区申请的空间,会很快结束
QPushButton *btn1 = new QPushButton; //调用无参构造
btn1->setText("按钮1"); //设置按钮文本
btn1->setParent(this); //设置父组件
btn1->resize(100, 40); //设置按钮大小
btn1->setEnabled(false); //设置按钮不可用状态
//定义按钮,使用有参构造函数
QPushButton *btn2 = new QPushButton(this);
btn2->setText("按钮2"); //设置文本信息
btn2->resize(btn1->size()); //设置大小为其他按钮的大小
//btn2->move(0, 40);
btn2->move(0, btn1->height()); //移动按钮位置,以其他按钮作为标准
//定义按钮,并初始化文本信息
QPushButton *btn3 = new QPushButton("按钮3", this);
btn3->resize(btn1->size()); //设置大小
btn3->move(btn1->width(), 0); //移动按钮
btn3->setIcon(QIcon("D:/22061QT/day2/03Login/icon/cancel.png")); //设置按钮的图标
//定义按钮,初始化图标
QPushButton *btn4 = new QPushButton(QIcon("D:/22061QT/day2/03Login/icon/cancel.png"),
"按钮4",
this);
btn4->resize(btn1->size());
btn4->move(btn1->width(), btn1->height());
//定义一个标签
QLabel *lab = new QLabel("爱好:", this);
lab->move(200,200);
}
Widget::~Widget()
{
delete ui;
}
.pro文件
QT += core gui texttospeech
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
——————————————————————————————————————————————————————————————————————————————————————————————
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPushButton>
#include <QTextToSpeech> //引入文本转语音的头文件
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public slots:
void close(); //声明自定义的槽函数
void read(); //声明阅读函数
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
//定义一个按钮
QPushButton *btn1;
//定义一个按钮
QPushButton *btn2;
//定义按钮3
QPushButton *btn3;
//实例化一个播报者
QTextToSpeech speecher;
};
#endif // WIDGET_H
——————————————————————————————————————————————————————————————————————————————————————————————
源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//给按钮申请空间
btn1 = new QPushButton("按钮1", this);
//当按钮一旦被按下,就会触发一个clicked的信号,我们可以将该信号,连接到对应的槽函数中,处理相关逻辑
//通过connect函数进行连接
//测试qt4版本的连接函数
connect(btn1, SIGNAL(clicked()), this, SLOT(close())); //不安全的连接
//给按钮2申请空间
btn2 = new QPushButton("按钮2", this);
btn2->move(btn1->width(), 0); //移动按钮
//当按钮一旦被按下,就会触发一个clicked的信号,我们可以将该信号,连接到对应的槽函数中,处理相关逻辑
//测试qt5版本的连接函数
connect(btn2, &QPushButton::clicked, this, &Widget::read); //安全的连接
//给btn3申请空间
btn3 = new QPushButton("按钮3", this);
btn3->move(0, btn1->height());
//测试使用Larmda表达式作为槽函数
connect(btn3, &QPushButton::clicked, [&](){
this->resize(500,400);
});
}
Widget::~Widget()
{
delete ui;
}
//自定义的槽函数
void Widget::close()
{
this->close();
}
//自定义阅读的槽函数
void Widget::read()
{
speecher.say(QString("今天是周二,天气晴,心情不错,又学到新知识了!!!"));
}
2.