本文主要是介绍解决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><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style type="text/css">
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: "\2610"; }
li.checked::marker { content: "\2612"; }
</style></head><body style=" font-family:'Microsoft YaHei UI'; font-size:9pt; font-weight:400; font-style:normal;">
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html></string></property></widget></item><item><layout class="QHBoxLayout" name="horizontalLayout"><item><widget class="QPushButton" name="pushButton_last"><property name="text"><string><-</string></property></widget></item><item><widget class="QPushButton" name="pushButton_next"><property name="text"><string>-></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函数显示中文乱码的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!