华清远见第六课程day10作业

保纯

//保存按钮对应的槽函数
void Widget::on_saveBtn_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,              
                                                    "保存文件",         
                                                    "./",              
                                                    "All(*.*);;Images (*.png *.xpm *.jpg);;Text files(*.txt);;XML files(*.xml)");       //过滤器
    //判断是否选中文件
    if(fileName.isNull())   
    {
        QMessageBox::information(this, "提示", "用户取消了保存文件");
        return;
    }
 
    qDebug()<<fileName;         
 
    //文件操作
    //1、实例化一个文件对象
    QFile file(fileName);
 
    //2、打开文件
    if(!file.isOpen())          
    {
        //调用打开文件操作
        if(!file.open(QFile::WriteOnly))
        {
            QMessageBox::critical(this, "失败", "文件打开失败");
            return;           
        }
    }
 
    //3、读写操作
    QString msg = ui->textEdit->toPlainText();
    file.write(msg.toLocal8Bit());
 
    //4、关闭文件
    file.close();
 
    QMessageBox::information(this, "提示", "文件保存成功");
 
}

到点播报文本框内容

头文件

  #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QTimerEvent>
#include<QTime>
#include<QPushButton>
#include<QDebug>
#include<QtDebug>
#include<iostream>
#include<QIcon>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include<QMessageBox>
#include <QTextEdit>
#include<QTextCharFormat>
#include <QTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void timerEvent(QTimerEvent *e)override;
private:
    Ui::MainWindow *ui;
    int timer_id;
    QLabel *systime;
    QTextEdit *saytext;
    QLineEdit *settime;
    QPushButton *start;
    QPushButton *stop;
    QTextToSpeech *tts;

private slots:
    void on_startBtn_clicked();

    void on_closeBtn_clicked();

};
#endif // MAINWINDOW_H
  

源文件

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(720,460);
    tts = new QTextToSpeech(this);
    timer_id=this->startTimer(1000);

    systime = new QLabel(this);
    systime->move(60,45);
    systime->resize(280,80);
    systime->setStyleSheet("background-color:rgb(222,222,222)");
    QTime sys_t = QTime::currentTime();
    QString ti = sys_t.toString("hh:mm:ss");
    this->systime->setText(ti);
    QFont font("Microsoft YaHei",20,20);
    systime->setFont(font);
    systime->setAlignment(Qt::AlignCenter);

    settime=new QLineEdit(this);
    settime->resize(230,40);
    settime->move(390,45);

    saytext=new QTextEdit(this);
    saytext->resize(570,250);
    saytext->move(systime->x(),systime->y()+130);

    start=new QPushButton("start",this);
    start->move(settime->x(),settime->y()+55);
    start->resize(100,35);
    connect(start,SIGNAL(clicked()),this,SLOT(on_startBtn_clicked()));

    stop=new QPushButton("stop",this);
    stop->move(start->x()+130,start->y());
    stop->resize(100,35);
    connect(stop,SIGNAL(clicked()),this,SLOT(on_closeBtn_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timerEvent(QTimerEvent *e){
    if(e->timerId()==timer_id){
        QTime sys_t = QTime::currentTime();
        QString ti = sys_t.toString("hh:mm:ss");
        this->systime->setText(ti);
        if(ti==settime->text()){
            tts->say(saytext->toPlainText());
        }
    }
}

void MainWindow::on_startBtn_clicked()
{
    timer_id = this->startTimer(1000);

}


void MainWindow::on_closeBtn_clicked()
{
    this->killTimer(timer_id);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值