Qt 打开指定网站/系统文件夹

本文介绍如何在Qt程序中使用QDesktopServices类的openUrl方法打开指定网站和本地文件夹,并修复QUrl::addQueryItem的一个Bug,确保参数正确传递。


本文转载自:http://blog.youkuaiyun.com/robertkun/article/details/7802977http://hi.baidu.com/xyhouse/item/ccfbe58634aac2eae496e0a6

一、QT打开指定网站和文件夹

在Qt程序中,如果要打开指定网站或系统中的文件夹,可以使用QDesktopServices类的openUrl方法。

详见http://qt-project.org/doc/qt-5/qdesktopservices.html

比如要打开Qt开发社区,相应的代码如下:

#include <QDesktopServices>
#include <QUrl>

QDesktopServices::openUrl(QUrl("http://qt-project.org/doc/qt-5/classes.html"));

要打开系统中的某个文件夹,如下:

QDesktopServices::openUrl(QUrl("file:///C:/Documents and Settings/All Users", QUrl::TolerantMode));


需要注意的是打开网址是http://,打开文件夹是file:///


在官网中关于QDesktopServices::openUrl的方法描述如下:

bool QDesktopServices::openUrl(constQUrl & url) [static]

Opens the given url in the appropriate Web browser for the user's desktop environment, and returnstrue if successful; otherwise returnsfalse.

If the URL is a reference to a local file (i.e., the URL scheme is "file") then it will be opened with a suitable application instead of a Web browser.

The following example opens a file on the Windows file system residing on a path that contains spaces:

QDesktopServices::openUrl(QUrl("file:///C:/Documents and Settings/All Users/Desktop", QUrl::TolerantMode));

 

二、Qt中QUrl::addQueryItem的Bug

在开发中,用到了如下语句:

QUrl url = QString("http://localhost/test")

url.addQueryItem("test", QString("abc+bcd"));

服务器一收,发现接收到的字符串变成了"abc bcd"。顿时郁闷了。

查了半天,找不着原因,上论坛问,很久也没人回答。

终于,有一位大牛解开了疑惑:

Qt should be encoding the literal '+' as %2B on your behalf. There is a bug that has been (I think incorrectly) closed as invalid:http://bugreports.qt.nokia.com/browse/QTBUG-10146

You can work around it with this:
Qt Code:
Switch viewurl.addEncodedQueryItem("another",QUrl::toPercentEncoding("abc+bcd"));

最后,推荐一下Qt的这个论坛,牛人挺多的。

http://www.qtcentre.org/content/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值