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);
        }
    }
  • 相关阅读:
    PostgreSQL pg_hba.conf 文件简析
    Centos 查看端口占用情况
    Nginx 从0开始学
    windows 杀死端口号对应进程
    MyBatis基础-05-缓存
    MyBatis基础-04-动态sql
    MyBatis基础-02
    SpringMVC基础-14-SpringMVC与Spring整合
    SpringMVC基础-13-SpringMVC运行流程
    SpringMVC基础-12-异常处理
  • 原文地址:https://www.cnblogs.com/xiaozoui11cl/p/12838418.html
Copyright © 2011-2022 走看看