本文轉載自:http://blog.csdn.net/robertkun/article/details/7802977和http://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));
在開發中,用到了如下語句:
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的這個論壇,牛人挺多的。