qt怎么在textedit中显示中文

2024-11-30 00:39:47
推荐回答(1个)
回答1:

void Form::fileOpenSlot()
{
QString path=QFileDialog::getOpenFileName(this,"打开文件","./","File(*.txt *.doc *.html)");//设置打开的文件路径
QFile in(path);
if(!in.open(QIODevice::ReadOnly | QIODevice::Text))qDebug()<<"Can't open the file!"< QTextCodec* codec=QTextCodec::codecForName("UTF-8");//静太函数codecForName设置为UTF-8编码模式默认不是这个格式的!
QByteArray Line=in.readAll();
QString str=codec->toUnicode(Line);//把Line转成UTF-8赋值给str 这里是关键
ui->textEdit->append(str);
in.close();
}