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();
            }
        }
}
相关推荐
吹什么轩1 小时前
c++复习:map和set的使用
开发语言·c++
必须得开心呀1 小时前
qt生成dump文件并定位异常
开发语言·qt
fpcc1 小时前
跟我学C++中级篇—内存流
开发语言·c++
吹什么轩3 小时前
c++复习:模拟实现list来更好的理解和应用list
开发语言·c++
AmyLin_20013 小时前
PDF 色彩保真工程实践【5】核心实现(下):拼装 Image XObject、页面资源与内容流
c++·pdf·sdk·颜色空间·印刷·icc·cmyk
乱七八糟的屋子4 小时前
MTL4 (Matrix Template Library) 超详细实战教程|C++高性能稠密/稀疏矩阵计算
c++·矩阵·稀疏矩阵·mtl4·matrixtemplate·c++线性代数·高性能数值计算
依然鸣5 小时前
PTA团体程序设计天梯赛L2真题讲解L2-045-048
数据结构·c++·经验分享·学习·算法·pat考试·pat
有点。5 小时前
C++深度优先搜索(二)
开发语言·c++·深度优先
思麟呀6 小时前
C++(七):std::any+std::string_view
开发语言·c++
小小龙学IT6 小时前
Taskflow:用一张“任务图“玩转现代 C++ 并行编程开源项
c++·开源·github