QT day3

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

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

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


void Widget::on_ziti_clicked()//字體按鈕的槽函數
{
    //直接調用Qfontdialog類中的靜態成員函數,getfont函數來調取系統提供的字體對話框
    bool ok;                                  //用於接受用戶是否選中了字體
    QFont f= QFontDialog::getFont(&ok,//返回是否选中字体
                        QFont("隶属",10,10,false),//初始字体的类型 大小 倾斜
                         this,                  //父组件
                         "选择字体");               //对话框标题
    //选中的字体进行使用
    if(ok)
    {
        //选中字体,可以把字体设置到文本上
       // ui->textEdit->setFont(f);
        ui->textEdit->setCurrentFont(f);//设置当前字体
    }
    else
    {
        //没选中字体
        QMessageBox::information(this,"提示","您取消了选择字体");
    }
}
                                    //字体颜色对话框

void Widget::on_yanse_clicked()//颜色按钮对应的槽函数
{
    //调用静态成员函数 获得系统颜色对话框
    QColor c=QColorDialog::getColor(QColor("pink"),
                                    this,
                                    "选择颜色");
    if(c.isValid())//对选中的颜色判断合法性
    {
        //颜色合法直接使用
        ui->textEdit->setTextColor(c);//选中字体改变颜色
        ui->textEdit->setTextBackgroundColor(c);//设置选中字体的背景颜色
    }
    else
    {
        //颜色不合法
        QMessageBox::information(this,"提示","颜色不合法");
    }
}
//打开文件对话框
void Widget::on_open_clicked()
{
    //调用QFliedialog的静态成员函数getopenfilename来获取选中文件的路径
    QString filename=QFileDialog::getOpenFileName(
    this,
    "选择文件",//父组件
     "./",//起始路劲
    "Image File(*.png *.jpg *bmp);;Text File(*.txt);;All(*.*)");//过滤器
    //判断是否有选中文件
    if(filename.isNull())
    {
        QMessageBox::information(this,"提示","你取消了文件");
    }
    qDebug()<<filename;


    //实例化一个文件对象
    QFile file(filename);       //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于
                                //该对象

    //判断文件是否存在
    if(!file.exists())
    {
        return;
    }

    //打开文件
    if(!file.open(QFile::ReadWrite))
    {
        return;
    }
    // 读取文件中的内容lll
    QByteArray mas=file.readAll();

    //将内容展示到 ui界面
    ui->textEdit->setText(mas);

    //关闭文件
    file.close();

}
void Widget::on_baocun_clicked()//保存文件
{
    QString filename=QFileDialog::getSaveFileName(
    this,
    "保存文件",//父组件
     "./",//起始路劲
    "Image File(*.png *.jpg *bmp);;Text File(*.txt);;All(*.*)");//过滤器
    //判断是否有选中文件
    if(filename.isNull())
    {
        QMessageBox::information(this,"提示","你取消了文件");
    }
    qDebug()<<filename;


    //创造一个实例对象
    QFile file(filename);       //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于
                                //该对象

    //    //判断文件是否存在
    //    if(!file.exists())
    //    {
    //        return;
    //    }

    //打开文件
    if(!file.open(QFile::WriteOnly))//QFile::Truncate))
    {
        return;
    }
    // 读取文件中的内容

    QString save=ui->textEdit->toPlainText();

    QByteArray sa=save.toUtf8();
    file.write(sa);

    //关闭文件
    file.close();

}
相关推荐
用户805533698035 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner5 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz10 天前
QML Hello World 入门示例
qt
xcyxiner13 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner13 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner14 天前
DicomViewer (添加模型类)3
qt
xcyxiner15 天前
DicomViewer (目录调整) 2
qt
xcyxiner15 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00616 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术16 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript