jimu 发表于 2016-12-12 12:18:35

邮件解码 乱码问题

本帖最后由 jimu 于 2016-12-12 14:13 编辑

QWebView 查询设置编码

ui.mailWebViewBrowser->settings()->defaultTextEncoding();
settings->setDefaultTextEncoding("UTF-8"); //设置编码格式
这样就成功改变了QwebView的编码格式。如果html文件是本地的,可以改变html文件的编码格式,将html头文件中的charset换为GB2312或GBK<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />

<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
QWebView如何 自动检测html内容编码?



jimu 发表于 2016-12-12 13:58:06


bool isgbk(char *s, size_t ns)
{
    if (ns > 2 && (uint8_t)*s >= 0x81 && (uint8_t)*s <= 0xfe
      && (
      ((uint8_t)*(s + 1) >= 0x80 && (uint8_t)*(s + 1) <= 0x7e)
      || ((uint8_t)*(s + 1) >= 0xa1 && (uint8_t)*(s + 1) <= 0xfe)
      )
      )
    {
      return true;
    }
    return false;
}

bool isGBKCode(const string& strIn)
{
    unsigned char ch1;
    unsigned char ch2;

    if (strIn.size() >= 2)
    {
      ch1 = (unsigned char)strIn.at(0);
      ch2 = (unsigned char)strIn.at(1);
      if (ch1 >= 129 && ch1 <= 254 && ch2 >= 64 && ch2 <= 254)
            return true;
      else return false;
    }
    else return false;
}

int isutf8(const char *s, size_t ns)
{
    uint8_t x = 0, i = 0, j = 0, nbytes = 0, n = 0;

    for (i = 1; i < 7; i++)
    {
      x = (uint8_t)(255 << i);
      if (((uint8_t)*s & x) == x)
      {
            n = nbytes = (8 - i);
            for (j = 0; (j < nbytes && j < ns); j++)
            {
                if ((uint8_t)s <= 0x80 && (uint8_t)s >= 0xc0)break;
                else n--;
            }
            if (n == 0) return nbytes;
      }
    }
    return 0;
}

jimu 发表于 2016-12-12 13:58:25

QWebView 支持的编码类型:参考QTextCodec
The supported encodings are:
Big5
Big5-HKSCS
CP949
EUC-JP
EUC-KR
GB18030
HP-ROMAN8
IBM 850
IBM 866
IBM 874
ISO 2022-JP
ISO 8859-1 to 10
ISO 8859-13 to 16
Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml
KOI8-R
KOI8-U
Macintosh
Shift-JIS
TIS-620
TSCII
UTF-8
UTF-16
UTF-16BE
UTF-16LE
UTF-32
UTF-32BE
UTF-32LE
Windows-1250 to 1258

jimu 发表于 2016-12-12 14:01:42

QWebView 显示本地HTML 文件webView->load(QUrl(QString("file:///c:\\a.html"));可能会导致a.html中有些使用相对路径的图片、js文件不能正常加载。使用如下代码即可webView->load(QUrl::fromLocalFile("c:\\a.html"));


页: [1]
查看完整版本: 邮件解码 乱码问题