QT&C++

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include<QFontDialog>//字体对话框
#include<QFont>//字体类
#include<QMessageBox>
#include<QColorDialog>
#include<QColor>
#include<QString>
#include<QFileDialog>
#include<QDebug>
#include<QFile>
#include<QInputDialog>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

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

//字体函数
void Widget::on_fontBtn_clicked()
{
    bool ok;
    QFont f = QFontDialog::getFont(&ok,
                         QFont("楷体",10,10),
                         this,
                         "选择字体");

    //对是否选中字体进行判断
    if(ok==true){
        //将选中字体设置到文本编辑器中
        ui->textEdit->setCurrentFont(f);
    }else{
        //全设置
        ui->textEdit->setFont(f);
    }

}

//颜色函数
void Widget::on_colorBtn_clicked()
{
    QColor c =QColorDialog::getColor(QColor("red"),
                                     this,
                                     "选择颜色");

    //判断
    if(c.isValid()){
        //将选中颜色,设置到文本编辑器中(字体色)
        ui->textEdit->setTextColor(c);
    }else{

    }



}

//文件函数
void Widget::on_openBtn_clicked()
{
    //参数:
    //父组件、文件名、文件路径、过滤器(可以不设置)
    //该函数可以不设置任何参数、默认打开当前路径
    QString name = QFileDialog::getOpenFileName(this,
                                                 "打开文件",
                                                 "D:\\ProjectWorkCpp\\Day208\\pictrue"
                                                 );
    qDebug()<<name;

    //文件IO操作
    QFile f;
    f.setFileName(name);//设置要管理的文件

    //打开文件
    //判断,存不存在
    if(!f.exists()){
        QMessageBox::information(this,"提示","文件不存在!");
    }else{
        if(!f.open(QFile::ReadOnly | QFile::WriteOnly))
         QMessageBox::information(this,"提示","打开失败");
    }

    //读取文件中数据
    QByteArray msg = f.readAll();

    //展示
    ui->textEdit->setText(QString(msg));

    //关闭
    f.close();

}

//输入函数
void Widget::on_inputBtn_clicked()
{
    QString name =  QInputDialog::getText(this,
                          "输入文本",
                          "请输入姓名");
    if(name!=NULL){
        //展示
        ui->textEdit->setText(QString(name));
    }

}

//另存为函数
void Widget::on_saveBtn_clicked()
{
    // 打开另存为对话框,让用户选择保存文件的路径
        QString filePath = QFileDialog::getSaveFileName(this, tr("另存为"), "", tr("文本文件 (*.txt)"));
        if (!filePath.isEmpty()) {
            QFile file(filePath);
            if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                QTextStream out(&file);
                // 这里假设要保存的内容是一段示例文本
                QString content = "这是要保存的示例文本。";
                out << content;
                file.close();
            }
        }
}
相关推荐
得物技术43 分钟前
搜索 C++ 引擎回归能力建设:从自测到工程化准出|得物技术
c++·后端·测试
xlp666hub21 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
会员源码网1 天前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
xlp666hub1 天前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
不想写代码的星星1 天前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++
xlp666hub2 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
不想写代码的星星2 天前
C++继承、组合、聚合:选错了是屎山,选对了是神器
c++
不想写代码的星星3 天前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
Felix_One4 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
樱木Plus5 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++