解决QTextBrowser控件调用setSource函数显示中文乱码的问题

本文主要是介绍解决QTextBrowser控件调用setSource函数显示中文乱码的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  自己发现的一个小技巧,有更好的方法欢迎大佬赐教。
  工程说明: 使用TextBrowser显示html可以进行html链接的导航,就是可以切换到上一链接界面或者下一个链接界面 但是我在使用的过程中发现,TextBrowser控件显示中文会出现乱码(textedit不会,不知道为什么),我是在windows平台上测试的,发现被加载的 html文件设置成utf16 LB编码格式就能正常显示,不过我已经完成了很多数量的html文件,一个一个的改或者找别的工作批量还是觉得不方便目前找到一种方法,在textBrowser控件执行setSource()函数后,再调用setHtml函数重新设置显示的文件这样不会出现中文乱码的情况。

  直接把工程源文件放上来了,直接拷贝用,另外测试的html文件就靠读者自备了。

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QUrl>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextCodec>//导航操作枚举值
enum NAVOPER{NAVOPER_NONE,NAVOPER_HOME,NAVOPER_PREV,NAVOPER_NEXT
};QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void InitTextBrowserShowFileName(const QString & _qstr);private slots:void on_pushButton_last_clicked();void on_pushButton_next_clicked();void on_pushButton_home_clicked();void SlotUrlChanged(const QUrl &);void SlotAnchorClicked(const QUrl &);private:Ui::MainWindow *ui;void SetTextBrowserShowHtml(NAVOPER _oper);QString m_curfilefullpathfortextbrowser;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);InitTextBrowserShowFileName("./testhtml/1-html.html");SetTextBrowserShowHtml(NAVOPER_NONE);connect(ui->textBrowser,SIGNAL(sourceChanged(const QUrl &)),this,SLOT(SlotUrlChanged(const QUrl &)));connect(ui->textBrowser,SIGNAL(anchorClicked(const QUrl &)),this,SLOT(SlotAnchorClicked(const QUrl &)));
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::InitTextBrowserShowFileName(const QString &_qstr)
{m_curfilefullpathfortextbrowser = _qstr;
}void MainWindow::on_pushButton_last_clicked()
{SetTextBrowserShowHtml(NAVOPER_PREV);
}void MainWindow::on_pushButton_next_clicked()
{SetTextBrowserShowHtml(NAVOPER_NEXT);
}void MainWindow::on_pushButton_home_clicked()
{SetTextBrowserShowHtml(NAVOPER_HOME);
}void MainWindow::SlotUrlChanged(const QUrl & _url)
{m_curfilefullpathfortextbrowser = _url.toString();
}void MainWindow::SlotAnchorClicked(const QUrl & _url)
{m_curfilefullpathfortextbrowser = _url.toString();SetTextBrowserShowHtml(NAVOPER_NONE);
}void MainWindow::SetTextBrowserShowHtml(NAVOPER _oper)
{if(NAVOPER_HOME == _oper){ui->textBrowser->home();}else if(NAVOPER_PREV == _oper){ui->textBrowser->backward();}else if(NAVOPER_NEXT == _oper){ui->textBrowser->forward();}else if(NAVOPER_NONE == _oper){}else{return;}ui->textBrowser->setSource(m_curfilefullpathfortextbrowser,QTextDocument::HtmlResource);qInfo()<<"[show file] "<<m_curfilefullpathfortextbrowser;QFile file(m_curfilefullpathfortextbrowser);file.open(QIODevice::ReadOnly);QString temp = file.readAll();ui->textBrowser->setHtml(temp);file.close();
}
//mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><layout class="QVBoxLayout" name="verticalLayout_2"><item><layout class="QVBoxLayout" name="verticalLayout"><item><widget class="QTextBrowser" name="textBrowser"><property name="html"><string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;meta charset=&quot;utf-8&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: &quot;\2610&quot;; }
li.checked::marker { content: &quot;\2612&quot;; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Microsoft YaHei UI'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string></property></widget></item><item><layout class="QHBoxLayout" name="horizontalLayout"><item><widget class="QPushButton" name="pushButton_last"><property name="text"><string>&lt;-</string></property></widget></item><item><widget class="QPushButton" name="pushButton_next"><property name="text"><string>-&gt;</string></property></widget></item><item><widget class="QPushButton" name="pushButton_home"><property name="text"><string>home</string></property></widget></item></layout></item></layout></item></layout></widget><widget class="QMenuBar" name="menubar"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>21</height></rect></property></widget><widget class="QStatusBar" name="statusbar"/></widget><resources/><connections/>
</ui>
//pro文件
QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

这篇关于解决QTextBrowser控件调用setSource函数显示中文乱码的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/314355

相关文章

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf