Qt --- Day03

css 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <pointsize>10</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QLabel" name="ltime">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>130</y>
     <width>211</width>
     <height>81</height>
    </rect>
   </property>
   <property name="sizePolicy">
    <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="font">
    <font>
     <pointsize>17</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">background-color: rgb(255, 255, 255);</string>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="timer">
   <property name="geometry">
    <rect>
     <x>370</x>
     <y>130</y>
     <width>171</width>
     <height>41</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
    </font>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QPushButton" name="start">
   <property name="geometry">
    <rect>
     <x>370</x>
     <y>180</y>
     <width>70</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>启动</string>
   </property>
  </widget>
  <widget class="QPushButton" name="stop">
   <property name="geometry">
    <rect>
     <x>460</x>
     <y>180</y>
     <width>70</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>停止</string>
   </property>
  </widget>
  <widget class="QTextEdit" name="textbox">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>260</y>
     <width>451</width>
     <height>231</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>16</pointsize>
    </font>
   </property>
  </widget>
  <widget class="QPushButton" name="load">
   <property name="geometry">
    <rect>
     <x>330</x>
     <y>520</y>
     <width>200</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>14</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>保存</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>520</y>
     <width>200</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>14</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>打开</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
cpp 复制代码
#include<QTime>
#include<QString>
#include<QFile>
#include<QMessageBox>
#include<QTextToSpeech>
#include<QDebug>
#include<QFileDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent *e) override;

private slots:
    void on_start_clicked();

    void on_stop_clicked();

    void on_load_clicked();

    void on_pushButton_clicked();

private:
    Ui::Widget *ui;

    //当时时间号
    int time_id;
    //查询时间号
    int timer_id;
    //语音
    QTextToSpeech * speech;
    //文件
    QFile *file;
};
#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
int i = 1;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    speech = new QTextToSpeech(this);
    time_id = this->startTimer(1000);




}

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


void Widget::on_start_clicked()
{
    QString t = ui->timer->text();

    if(t==NULL)
        QMessageBox::critical(this,"错误","请输入正确时间");
    else
        timer_id = this->startTimer(100);
}

void Widget::on_stop_clicked()
{
    killTimer(timer_id);
}
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == time_id)
    {
        QTime time = QTime::currentTime();
        QString s = time.toString("hh:mm:ss");
        ui->ltime->setText(s);
    }
    if(e->timerId() == timer_id)
    {


        if(ui->ltime->text() == ui->timer->text())
        {

            speech->say(ui->textbox->document()->toPlainText());
            if(i == 5)
            {
                i = 1;
                killTimer(timer_id);
            }
        }
    }
}

void Widget::on_load_clicked()
{
    QString fname = QFileDialog::getSaveFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
    if(fname!=NULL)
    {
        //实例化一个文件对象
        QFile file(fname);
        //存储内容
        QString ba;
        //打开文件
        ba = ui->textbox->document()->toPlainText();
        if(!file.isOpen())
            if(!file.open(QIODevice::WriteOnly))
            {
                QMessageBox::critical(this,"","打开文件失败");
                return;
            }
        file.write(ba.toUtf8());
        file.close();
    }
}

void Widget::on_pushButton_clicked()
{
    QString fname = QFileDialog::getOpenFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
    if(fname!=NULL)
    {
        //实例化一个文件对象
        QFile file(fname);
        //存储内容
        QByteArray ba;
        //打开文件
        if(!file.isOpen())
            if(!file.open(QIODevice::ReadOnly))
            {
                QMessageBox::critical(this,"","打开文件失败");
                return;
            }
        ba = file.readAll();
        file.close();

        ui->textbox->setText(ba);
    }
    else
        ui->textbox->setText("时间到");
}
cpp 复制代码
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
相关推荐
蒸蒸yyyyzwd21 分钟前
cpp对象模型学习笔记1.1-2.8
java·笔记·学习
银发控、1 小时前
MySQL联合索引
数据库·mysql
予枫的编程笔记1 小时前
【MySQL修炼篇】从踩坑到精通:事务隔离级别的3大异常(脏读/幻读/不可重复读)解决方案
数据库·mysql·后端开发·数据库事务·事务隔离级别·rr级别·脏读幻读不可重复读
程序员徐师兄1 小时前
Windows JDK11 下载安装教程,适合新手
java·windows·jdk11 下载安装·jdk11 下载教程
RANCE_atttackkk1 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
五岳2 小时前
DTS按业务场景批量迁移阿里云MySQL表实战(下):迁移管理平台设计与实现
java·应用·dts
一起养小猫2 小时前
Flutter for OpenHarmony 实战:记账应用数据统计与可视化
开发语言·jvm·数据库·flutter·信息可视化·harmonyos
世界尽头与你2 小时前
(修复方案)CVE-2023-22047: Oracle PeopleSoft Enterprise PeopleTools 未授权访问漏洞
数据库·安全·oracle·渗透测试
韩立学长2 小时前
【开题答辩实录分享】以《智能大学宿舍管理系统的设计与实现》为例进行选题答辩实录分享
数据库·spring boot·后端
zhougl9962 小时前
Java 所有关键字及规范分类
java·开发语言