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

时间:2021-01-11 16:31:58


本文转载自:http://blog.csdn.net/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/