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();
            }
        }
}
相关推荐
宛 禾11 分钟前
Qt 控件入门:QLabel 从拖拽到对象树,一篇讲透
c++·qt·学习
hetao173383723 分钟前
联盟场-提高组-R7 & ZYZSC-S-Round 11
c++·算法
程序喵大人25 分钟前
【C++入门】编译链接模型 - 07 链接器怎样把符号对上号
开发语言·c++·编译器·编译链接·链接器
局部有解1 小时前
Hot100 - 三数之和
数据结构·c++·算法
longlongzihan1 小时前
C++ 常用数据结构速查:vector、stack、queue、list、树、图、哈希表等声明与核心操作
数据结构·c++·list
别动我齐刘海1 小时前
实时控制学习2
linux·c++·人工智能·学习·算法
hetao17338373 小时前
校内场-提高组 ZYZSC-S-Round 2
c++·算法
hetao17338373 小时前
2026-10-14 hetao1733837 的刷题记录
c++·算法
浅念-4 小时前
动态规划:线性DP——多状态DP与子数组专题笔记
c++·笔记·算法·leetcode·职场和发展·动态规划·力扣
打工仔折腾 AI5 小时前
Qt网络编程实战:UDP、TCP到HTTP的落地经验与选型
网络·人工智能·后端·python·qt·udp·ai agent 实战