如何存储中文到文件中,解决乱码问题

void MainWindow::saveFile()
{

    QStringList lines;
    lines.append("lines 1 ");

    lines.append(QString::fromLocal8Bit("中文测试x"));

    QFile file( "/root/program/text1.txt" );
    if ( file.open( QIODevice::WriteOnly ) )
    {
        QTextStream stream( &file);

        for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
            stream << *it << "/n";

        QString str;
       
        //控件中文
        stream << ui->textEdit->toPlainText() <<endl;
        str = ui->textEdit->toPlainText();
        stream <<str<<endl;

        //写中文的方法
        QString hint;
        hint = hint.fromLocal8Bit("哈哈哈");
        stream<<hint<<endl;
        file.close();
    }
}

你可能感兴趣的:(如何存储中文到文件中,解决乱码问题)