创建一个项目,提供三个按钮,第一个按钮实现播报第二个按钮的内容,播报结束后,设置自己不可用。第二个按钮的内容是关闭,实现功能是关掉整个项目,第三个按钮功能是将第一个按钮设置为可以状态。
.h
#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
signals:
void singal_1();
public slots:
void spkMes();
void exitMes();
public:
Widget(QWidget *parent = 0);
~Widget();
//定义一个按钮函数
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
//定义一个播报
QTextToSpeech speech;
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
2.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
void Widget::spkMes()
{
speech.say(btn2->text());
btn1->setEnabled(false);
}
void Widget::exitMes()
{
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->resize(1024,660);
this->setMaximumSize(1400,980);
this->setMinimumSize(300,400);
this->setFixedSize(1000,800);
//设置窗口标题
this->setWindowTitle("第一个窗口");
//获取标题
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();
btn1->setParent(this);
btn1->resize(75,30);
btn1->move(0,height()/2);
btn2 = new QPushButton(this);
btn2->move(btn1->width(),height()/2);
btn2->resize(btn1->size());
btn2->setText("德玛西亚");
btn3 = new QPushButton("第三个",this);
btn3->resize(btn1->size());
btn3->move(btn1->width()+btn2->width(), height()/2);
//链接btn1发射的信号给spkMas处理
connect(btn1,SIGNAL(clicked()),this,SLOT(spkMes()));
//disconnect(btn1,SIGNAL(clicked()),this,SLOT(spkMes()));
connect(btn2,&QPushButton::clicked,[&]()
{
this->close();
});
connect(btn3,&QPushButton::clicked,[&]()
{
btn1->setEnabled(true);
});
}
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();
}
效果图: