下午写程序中遇到几个小细节,需要在这里记录一下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
QProcess *process =
new
QProcess(
this
);
QFileInfo fileinfo(appUrl);
QString appPath = QApplication::applicationDirPath()+SAVEDIR+
"/"
+fileinfo.fileName();
bool
res = process->startDetached(appPath,QStringList());
if
(res)
{
accept();
}
else
{
MyHelper::ShowMessageBoxError(
"下载完成但自动升级失败,请手工安装升级包!"
);
QDesktopServices::openUrl(QUrl::fromLocalFile(QApplication::applicationDirPath()+SAVEDIR));
reject();
}
|
- 细节1:
原本代码中是这样写的:process->startDetached(appPath);
测试环境没任何问题,但是生产环境却不能调用外部程序,最终发现生产环境的文件路径在C:\Program Files (x86)\...
带有空格,造成调用失败,于是google之后尝试process->startDetached(appPath,QStringList());
问题得到解决。
- 细节2:
QDesktopServices::openUrl(QUrl(QApplication::applicationDirPath()+SAVEDIR));
这地方同样存在这样的问题,如果使用QUrl::fromLocalFile进行处理,就可以正常打开文件夹:QDesktopServices::openUrl(QUrl::fromLocalFile(QApplication::applicationDirPath()+SAVEDIR));
除此之外,还有一个和这段代码无关的问题:Qt调用VC写的动态库,VC需要用C的方式输出函数,否则MinGW32编译过程会报错,即:
VC DLL头文件示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#ifndef CDOUBLEREADERDLL
#ifdef CDOUBLEREADERDLL_EXPORTS
#define CDOUBLEREADERDLL extern "C" __declspec(dllexport)
#else
#define CDOUBLEREADERDLL extern "C" __declspec(dllimport)
#endif
#endif
# define BUFFERSIZE 20
# define READTIMEOUT 1000
// 打开串口
CDOUBLEREADERDLL
bool
_stdcall RD_Open(
int
iPort,unsigned
long
iBaudRate);
...
|
Qt中调用示例:
1
2
3
4
|
extern
"C"
{
#include "drivers/CdoubleReaderDll.h"
}
|
转载请注明:梧桐树下 » Qt打开外部程序和文件夹需要注意的细节
http://www.pfeng.org/archives/808