创建一个项目,提供三个按钮,第一个按钮实现播报第二个按钮的内容,播报结束后,设置自己不可用。第二个按钮的内容是关闭,实现功能是关掉整个项目,第三个按钮功能是将第一个按钮设置为可以状态
MyWnd.h
#ifndef MYWND_H
#define MYWND_H
#include <QMainWindow>
#include<QPushButton>
#include<QTextToSpeech>
class MyWnd : public QMainWindow
{
Q_OBJECT
signals:
void signal_1();
public slots:
void showMes();
void say_mes();
public:
MyWnd(QWidget *parent = 0);
~MyWnd();
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QTextToSpeech speech;
};
#endif // MYWND_H
mywnd.cpp
#include "mywnd.h"
#include<QDebug>
#include<QtTextToSpeech>
void MyWnd::showMes()
{
btn1->setEnabled(true);
}
void MyWnd::say_mes()
{
speech.say(btn2->text());
btn1->setEnabled(false);
}
MyWnd::MyWnd(QWidget *parent)
: QMainWindow(parent)
{
this->resize(1024,768); //重新设置主控件大小
this->setMaximumSize(1500,1000); //设置最大尺寸
this->setMinimumSize(500,200); //设置最小尺寸
this->setFixedSize(1000,800); //设置固定尺寸
//设置窗口标题
this->setWindowTitle("FirstUi");
//获取标题
QString title=this->windowTitle(); //得到窗口标题
qDebug()<<title;
//设置背景色
this->setBackgroundRole(QPalette::Dark);
this->setAutoFillBackground(true);
this->move(50,50); //移动位置
//输出高度宽度
qDebug()<<"width = "<<this->width()<<"height = "<<this->height();
//输出坐标点
qDebug()<<"坐标点: "<<this->x()<<","<<this->y();
btn1= new QPushButton(this);
btn1->resize(75,30);
btn1->move(0,height()/2);
btn1->setText("播报");
btn2=new QPushButton(this);
btn2->resize(btn1->size());
btn2->move(btn1->width(),height()/2);
btn2->setText("关闭");
btn3=new QPushButton("可用",this);
btn3->resize(btn1->size());
btn3->move(btn1->width()+btn2->width(),height()/2);
//connect(btn1,SIGNAL(clicked()),this,SLOT(showMes())); //QT4版本
//connect(btn1,&QPushButton::clicked,this,&MyWnd::showMes); //QT5版本
/*connect(btn1,&QPushButton::clicked,[&](){
btn2->setText("222");
});*/
connect(btn1, &QPushButton::clicked, [&](){ //使用Larmda表达式当做槽函数
emit signal_1();
});
connect(this, &MyWnd::signal_1, this, &MyWnd::say_mes);
//将自定义信号连接到自定义槽中
connect(btn2,&QPushButton::clicked,[&](){
this->close();
});
connect(btn3,&QPushButton::clicked,this,&MyWnd::showMes);
}
MyWnd::~MyWnd()
{
}