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);
    }
}
原文地址:https://www.cnblogs.com/xiaozoui11cl/p/12838418.html