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();
            }
        }
}
相关推荐
*TQK*14 分钟前
双指针算法介绍+算法练习(2025)
c++·学习·算法·双指针
掘了17 分钟前
C++ 程序员应该了解的 Linux 命令
linux·c++·后端
郭涤生22 分钟前
并行算法_第十章_《C++并发编程实战》笔记
c++·算法·并发编程
newki1 小时前
学习笔记,C/C++编译相关概念与相关示例
android·c++·c
UpUpUp……2 小时前
Linux操作系统下Git的使用详细步骤
linux·运维·c++·git
橘子真甜~2 小时前
36.C++二叉树进阶5(平衡二叉搜索树 - 红黑树及其插入操作图解)
开发语言·数据结构·c++·算法·面试·二叉树·红黑树
十年一梦实验室2 小时前
详解 C++ 左值和右值对象 左值引用和右值引用
开发语言·c++
十年一梦实验室2 小时前
C++ 输出指针地址 输出指针数据
数据结构·c++·算法
码农客栈3 小时前
qt+opengl 播放yuv视频
qt·音视频
LuckyRich13 小时前
【高并发内存池】细节处理 + 性能优化 + 总结
c++·缓存·性能优化