为什么当从html文件读取Unicode内容时,Unicode字体在QTextBrowser中无法正确显示? [英] Why Unicode fonts are not showing properly in the QTextBrowser when Unicode contents are read from an html file?

查看:669
本文介绍了为什么当从html文件读取Unicode内容时,Unicode字体在QTextBrowser中无法正确显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 html 文件。该文件基本上包含 Unicode 文本,如下所示:

 < b> akko- sati kruś),akkhāti(ā+khyā),abbahati(ā+bṛh)< / b> 

QTextBrowser 不会解释 Unicode 字体。因此, QTextBrowser 会显示以下内容:

  akko- sati(Ä+kruÅ>),akkhÄti  QTextBrowser        strong>正确解释了 html 标记。但是 Unicode 字体有什么问题?



以下是我的代码,用于阅读和填充Unicode内容:

  void MainWindow :: populateTextBrowser(const QModelIndex& index)
{
QFile file(Data\\+ index。 data()。toString()+.html);
if(!file.open(QFile :: ReadOnly | QFile :: Text)){
statusBar() - > showMessage(无法打开文件:+ file.fileName());
}
QTextStream textStream1(& file);

QString string =< meta http-equiv ='Content-Type'content ='text / html; charset = utf-8'/>< link rel ='stylesheet'type = 'text / css'href ='Data / Accessories / qss.css'/>;
string + = textStream1.readAll();

ui-> textBrowser-> setHtml(string);但是,如果我没有阅读 Unicode (Unicode
),则可以使用 来自 html 文件的内容,但直接将它们输入到参数中,则只能解释 Unicode 字体。例如,如果我做如下,它是很好:

  ui-> gtbtextBrowser-> setHtml(< b> ; akko-sati(ā+kruś),akkhāti(ā+khyā),abbahati(ā+bṛh)< / b>);如何从 html 中读取 Unicode 的内容   strong>文件并将其显示在 QTextBrowser 中? 



如果有人向我显示代码中的错误部分,或告诉我更好的解决问题的方法,我将非常感谢。


<你读一个二进制文件到QString,但不告诉程序,哪些字节对应于哪个unicode字符,即你没有指定编码aka。



要调试您的问题,请询问 QTextStream 默认使用的代码:

  QTextStream textStream1(& file); 
qDebug()<< textStream1.codec() - > name();



在我的Linux系统上,这已经是UTF-8,但它可能在你的系统上不同。要强制QTextStream将输入解释为UTF-8,请使用 QTextStream :: setCodec


I am reading an html file. The file basically contains Unicode texts as follows:

<b>akko- sati (ā + kruś), akkhāti (ā + khyā), abbahati (ā + bṛh)</b>

But the QTextBrowser is not interpreting the Unicode fonts. So the QTextBrowser shows them as follows:

akko- sati (Ä + kruÅ›), akkhÄti (Ä + khyÄ), abbahati (Ä + bá¹›h)

The QTextBrowser is correctly interpreting the html tags. But what’s wrong with the Unicode fonts?

Following are my codes for reading and populating the Unicode contents:

void MainWindow::populateTextBrowser(const QModelIndex &index)
{
    QFile file("Data\\" + index.data().toString() + ".html");
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
         statusBar()->showMessage("Cannot open file: " + file.fileName());
    }
    QTextStream textStream1(&file);

    QString string = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><link rel='stylesheet' type='text/css' href='Data/Accessories/qss.css' />";
    string += textStream1.readAll();

    ui->textBrowser->setHtml(string); 
} 

However, if I do not read the Unicode content from an html file but directly type them into the parameter, then only it interprets the Unicode fonts. For example, if I do as follows it is fine:

ui->textBrowser->setHtml("<b>akko- sati (ā + kruś), akkhāti (ā + khyā), abbahati (ā + bṛh)</b>");

How can I read the Unicode contents from html files and show them in the QTextBrowser?

I shall be very thankful if someone shows me the buggy parts in my codes or tells me a better way of solving my problem.

解决方案

You read a binary file into QString but do not tell the program, which bytes correspond to which unicode character, i.e. you don't specify the "encoding" aka. "codec".

To debug your problem, ask QTextStream which codes it uses by default:

QTextStream textStream1(&file);
qDebug() << textStream1.codec()->name();

On my Linux system, that is already "UTF-8" but it might be different on your system. To force QTextStream interpreting the input as UTF-8, use QTextStream::setCodec.

这篇关于为什么当从html文件读取Unicode内容时,Unicode字体在QTextBrowser中无法正确显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆