现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

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

2014-12-09 16:35 工业·编程 ⁄ 共 1440字 ⁄ 字号 暂无评论

一、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")); 

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

[cpp] view plaincopy

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论坛,牛人挺多的。

给我留言

留言无头像?