`
king_tt
  • 浏览: 2099813 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Qt进行UrlEncode/UrlDecode(URL编码/解码)

 
阅读更多

为了让包含中文的 URL 可以使用,需要进行 UrlEncode 编码。Java 中有现成的类库可以使用,其实我们 Qt 进行网络编程时,框架中也带了 UrlEncode 的功能。

下面是我写的一个小程序,带图形界面的,可以对包含中文的 URL 进行 UrlEncode 。

头文件:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void onConvert();

protected:
    void resizeEvent(QResizeEvent *);
private:
    QLabel *m_labelInputPrompt;
    QLineEdit *m_editInput;
    QLabel *m_labelOutputPrompt;
    QLabel *m_labelOutput;
    QPushButton * m_btnConvert;
};

#endif // WIDGET_H

源文件:

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout * layoutInput = new QHBoxLayout();
    m_labelInputPrompt = new QLabel(QString::fromLocal8Bit("请输入汉字:"));
    m_editInput = new QLineEdit();
    m_btnConvert = new QPushButton(QString::fromLocal8Bit("转换"));
    layoutInput->addWidget(m_labelInputPrompt, 1);
    layoutInput->addWidget(m_editInput, 2);
    layoutInput->addWidget(m_btnConvert);

    QHBoxLayout * layoutOutput = new QHBoxLayout();
    m_labelOutputPrompt = new QLabel(QString::fromLocal8Bit("GB2312 Percent Encode:"));
    m_labelOutput = new QLabel();
    m_labelOutput->setFrameShape(QFrame::Box);
    layoutOutput->addWidget(m_labelOutputPrompt);
    layoutOutput->addWidget(m_labelOutput);

    QVBoxLayout * layout = new QVBoxLayout;
    layout->addLayout(layoutInput);
    layout->addLayout(layoutOutput);

    setLayout(layout);

    connect(m_btnConvert, SIGNAL(clicked()), this, SLOT(onConvert()));
}

Widget::~Widget()
{

}

void Widget::resizeEvent(QResizeEvent *)
{

}

void Widget::onConvert()
{
    QString strInput = m_editInput->text();
    if(strInput.isEmpty())
    {
        return;
    }
    QTextCodec * codecGB2312 = QTextCodec::codecForName("GB2312");
    QByteArray byteArrayGB2312 = codecGB2312->fromUnicode(strInput);
    QByteArray byteArrayPercentEncoded = byteArrayGB2312.toPercentEncoding();
    m_labelOutput->setText(QString(byteArrayPercentEncoded));
}

上面的代码,核心的就是 onConvert() 函数,其它的都是搭建界面需要的。

下面是 main() 函数:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //set codecs to UTF-8
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    QFont font("Microsoft yahei");
    font.setPixelSize(28);
    a.setFont(font);

    Widget w;
    w.show();

    return a.exec();
}

main() 函数很简单,启动界面而已。

在 onConvert() 函数中,我们从编辑框获取文本,构建一个 GB2312 编码的 TextCodec ,然后使用其 fromUnicode() 方法将得到的文本转化为使用 GB2312 编码过的 QByteArray ,再使用 QByteArray 的 toPercentEncoding() 方法转换成正常的 URL 格式。

对于包含汉字的 URL ,汉字的编码常用的有两种 GB2312 和 UTF-8 ,本文是用采用 GB2312 编码进行转换。

如果是 UTF-8 ,更简单,在上面的程序上稍作修改,不用构造 GB2312 的 TextCodec ,直接使用从编辑框获取的 QString 对象的 toUtf8() 方法得到 QByteArray,然后调用 toPercentEncoding() 方法即可。 onConvert() 代码如下:

    QString strInput = m_editInput->text();
    if(strInput.isEmpty())
    {
        return;
    }

    QByteArray ba = strInput.toUtf8();
    QByteArray byteArrayPercentEncoded = ba.toPercentEncoding();
    m_labelOutput->setText(QString(byteArrayPercentEncoded));


至于 UrlDecode ,和 UrlEncode 过程是相反的,就不再细说了。

一个小插曲,我的小程序原本是用 Qt 4.x 写的,今天发现在 Qt 5.2.0 上,QTextCodec 的一些方法没有了,如 setCodecForTr / setCodecForCStrings 。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics