解决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

相关文章

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编