zoukankan      html  css  js  c++  java
  • Qt 文件操作以及字体颜色选择

    void MyText::slot_open()
    {
        //选择文件
        QString path = QFileDialog::getOpenFileName(this,"选择文件",":/","头文件(*.h);;源文件(*.cpp);;所有文件(*.*)");
        if(!path.isEmpty())
        {
            //打开文件
            QFile file(path);
            if(file.open(QIODevice::ReadOnly))
            {
                QTextStream stream(&file);  //创建字符流对象
                QString str;
                while(!stream.atEnd())  //没有读到文件结尾
                {
                    //读取文件内容到文本域
                    str = stream.readLine(128);
                    this->textedit->append(str);    //将字符串添加到文本域
                }
            }
            file.close();
        }
    }
    void MyText::slot_save()
    {
        //获取文本域的内容
        QString content = this->textedit->toPlainText();
        //获取要保存的文件路径
        QString path = QFileDialog::getSaveFileName(this,"输入文件名",":/","头文件(*.h);;源文件(*.cpp)");
        if(!path.isEmpty())
        {
            //打开文件
            QFile file(path);
            if(file.open(QIODevice::WriteOnly))
            {
                //写入内容
                QTextStream stream(&file);
                stream << content;
                stream.flush();
            }
            file.close();
        }
    }  
    void MyText::slot_color()  //字体颜色的选择
    {
        QColor color = QColorDialog::getColor(Qt::black,this,"选择颜色");
        if(color.isValid())
        {
            QPalette pale = this->textedit->palette();
            pale.setColor(QPalette::Text,color);
            this->textedit->setPalette(pale);
        }
    }
  • 相关阅读:
    前端 --> CSS基础
    前端 css 补充内容
    前端 ---> HTML
    MySQL数据库 -- Navicat、pycharm连接数据库
    mysql数据库 --表查询
    IOC Unity
    泛型2
    泛型1
    特性 Attribute
    里氏替换原则
  • 原文地址:https://www.cnblogs.com/xiaozoui11cl/p/12838418.html
Copyright © 2011-2022 走看看