Qtday3

完成文本编辑器的保存工作

.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_fontBtn_clicked()
{
    bool ok;//返回用户是否选中字体
    QFont f=QFontDialog::getFont(&ok,QFont("隶书",10,10,false),
                                 this,"选择字体");
    //对ok进行判断,判断用户是否选中字体了
    if(ok)
    {
        //用户选中字体了
        //将选中的字体,设置到文字上
        //ui->textEdit->setFont(f);//设置全部字体文字
        ui->textEdit->setCurrentFont(f);//设置选中的字体字体


    }
    else
    {
        //用户取消了选中字体
        QMessageBox::information(this,"取消","用户取消的选择字体");

    }
}
//颜色对话框对应的槽函数
void MainWindow::on_colorBtn_clicked()
{
    QColor c=QColorDialog::getColor(QColor("green"),//初始颜色
                                    this,
                                    "选择颜色");
    //判断c的合法性
    if(c.isValid())
    {
        //用户选择的颜色
        //将用户选择的颜色作业到文本上
       ui->textEdit->setTextColor(c);//设置选中的颜色
       //ui->textEdit->setTextBackgroundColor(c); //设置背景颜色
    }else
    {
        QMessageBox::information(this,"取消","用户取消的选择颜色");

    }
}
//打开按钮找到的槽函数
void MainWindow::on_openBtn_clicked()
{
    QString fileName =QFileDialog::getOpenFileName(this,
                                                   "选择文件",
                                                   "./",
                                                   "All(*.*);;Image(*.png *.xpm *.jpg);; "
                                                   "Text files (*.txt);;files(*.xml)");
    //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","用户取消了选中文件");
        return;
    }
    qDebug()<<fileName;//得到文件路径
    //文件操作
    //1.实例化
    QFile file(fileName);
    //打开文件
    if(!file.isOpen())//该文件没有被打开则打开文件
    {
        //调用打开文件操作
        if(!file.open(QFile::ReadWrite))
        {
            QMessageBox::critical(this,"失败","文件打开失败");
            return;
        }
    }
    //3.读写内容
    QByteArray msg=file.readAll();
    //4.关闭文件
    file.close();
    //将读取的文本展示在ui界面
    ui->textEdit->setText(msg);
}
//文件保存槽函数
void MainWindow::on_saveBtn_clicked()
{

    QString fileName = QFileDialog::getSaveFileName(this,                     //父组件
                                                        "保存文件",                //对话框标题
                                                        "./",                     //起始路径
                                                        "all file(*.*);;Text(*.txt);;Image(*.png,*.jpg,*.gif)");   //过滤器

    //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","用户取消了选中文件");
        return;
    }
    qDebug()<<fileName;//得到文件路径
    //文件操作
    //1.实例化
    QFile file(fileName);
    //2.打开文件
    if(!file.open(QFile::Append))//该文件没有被打开则打开文件
    {

        return;
    }
    //3.获取textEdit中的内容
    QString msg=ui->textEdit->toPlainText();
    //4.textEdit将内容写入
    file.write(msg.toLocal8Bit());
    //关闭文件
    file.close();
}

.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QWidget>
#include<QFont>
#include<QMessageBox>
#include<QFontDialog>
#include<QColor>//属于颜色类
#include<QColorDialog>
#include<QFileDialog>
#include<QFile>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_fontBtn_clicked();

    void on_colorBtn_clicked();

    void on_openBtn_clicked();

    void on_saveBtn_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->timer=new QTimer;
    timeid=startTimer(100);

   
    //尺寸
    this->setFixedSize(800,600);
    //更改标题
    this->setWindowTitle("闹钟");
    //连接定时器信号和自定义的槽函数

    connect(timer,&QTimer::timeout,this,&Widget::LCD_Show);
}

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

//启动的槽函数
void Widget::on_upBtn_clicked()
{
    timer->start(100);
    ui->pushtime->setReadOnly(true);
    ui->textt->setReadOnly(true);

}
void Widget::LCD_Show()
{
    if(ui->pushtime->text()==ui->nowtime->text())
       {
           speech =new QTextToSpeech(this);
           QString text=ui->textt->toPlainText();
           //qDebug()<<text;
           speech->say(text);
       }

}

//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==timeid)
    {
        QDateTime sysDateTime=QDateTime::currentDateTime();
        //讲QTime类对象转化为字符串
        QString t=sysDateTime.toString("hh:mm:ss");
        //展示到ui界面
        ui->nowtime->setText(t);
    }

}
//停止的槽函数
void Widget::on_downBtn_clicked()
{
    QMessageBox box(QMessageBox::Warning,"Warning","是否要重设闹钟",QMessageBox::Yes|QMessageBox::No);
           int ret = box.exec();
           if(ret==QMessageBox::Yes)
           {
               //关闭定时器
               timer->stop();
               //定时器关闭后行编辑器和文本编辑器重新有效
               ui->pushtime->setReadOnly(false);
               ui->textt->setReadOnly(false);
               //清空之前设置的行文本编辑器
               ui->pushtime->clear();
               //停止语音播报
               speech->stop();


           }

}

.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H


#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include <QDebug>
#include <QTextToSpeech>
#include <QMessageBox>


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 *event)override;
    //void paintEvent(QPaintEvent *event) override;

private slots:
    void on_upBtn_clicked();

    void on_downBtn_clicked();


    void LCD_Show();
private:
    Ui::Widget *ui;


    QTimer *timer;//显示屏显示时间

    int timeid;//定义闹钟定时器
    //定义文本输入框的播报者
      QTextToSpeech *speech;

};
#endif // WIDGET_H

.pro

cpp 复制代码
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
相关推荐
其实防守也摸鱼3 天前
GDB安装与配置(保姆级教程)【Linux、Windows系统】
linux·运维·windows·命令模式·工具·虚拟机·调试
其实防守也摸鱼9 天前
无线网络安全---WLAN相关安全工具--kali(理论附题目)
linux·安全·web安全·学习笔记·kali·命令模式·wlan
sg_knight11 天前
设计模式实战:命令模式(Command)
python·设计模式·命令模式
yaaakaaang11 天前
十四、命令模式
java·命令模式
无籽西瓜a11 天前
【西瓜带你学设计模式 | 第十八期 - 命令模式】命令模式 —— 请求封装与撤销实现、优缺点与适用场景
java·后端·设计模式·软件工程·命令模式
23.20 天前
【Linux】grep -F 及 双横线--的妙用
linux·命令模式
摸鱼仙人~22 天前
快照模式 vs 命令模式:一篇分清什么时候用谁
命令模式
2301_7644413322 天前
Dify工作流中实现查询优化(QO):将查询复杂度分类法与QOL框架融入工作流
人工智能·语言模型·自然语言处理·命令模式
fe7tQnVan22 天前
三大 Agent-UI 协议深度剖析:AG-UI、A2UI 与 MCP-UI 的设计哲学与工程实践
ui·状态模式·命令模式
程序员小寒24 天前
JavaScript设计模式(八):命令模式实现与应用
前端·javascript·设计模式·ecmascript·命令模式