您的位置:首页 > 编程语言 > Qt开发

Qt程序开机启动的怪现象————无法正常显示程序皮肤

2014-10-14 15:58 405 查看
事情很简单:最近公司项目在做即时通讯软件,类似QQ。该软件应该支持开机启动这样的常用功能。但是实际上开发该功能的时候碰到了个问题:开机启动程序无法正常加载皮肤文件。

这个问题让我头疼了很久啊。最终确定问题出现在程序的打包皮肤文件上。因为界面使用的是qt所以,皮肤等资源文件都是应该放在qrc文件中进行统一管理的。但是实际上该程序的资源文件却都是在外面的。这样的结果就是正常启动程序没有问题,开机启动就会加载不上皮肤文件。

下面就是我想到的解决方法:

方法一:修改qrc文件,将所有的资源文件都添加到qrc文件中进行管理。这样问题就应该能够解决。另外我找到了官方的style的具体使用方法,具体使用方式和我说的基本一致。

方法二(我师父想出来的):修改程序运行时的所在目录。这个不太好理解。大概是windows启动的时候,程序虽然启动了,但是程序所在的文件夹却没有被识别,导致开机启动无法正常加载皮肤资源的等文件。具体解决办法就是在程序的main函数中设置程序所在目录。请看代码选段:

//获取当前exe文件所在路径
std::string get_app_run_path()
{
char szFilePath[MAX_PATH + 1];
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, ('\\')))[1] = 0;//删除文件名,只获得路径
return std::string(szFilePath);
}
bool GetCurrentProcessDirectory(std::wstring &wstrCurrentProcessPath)
{
bool is_success = false;

do
{
WCHAR *lpProcessPath = (WCHAR* )malloc(sizeof(WCHAR)*MAX_PATH);
if (lpProcessPath)
{
ZeroMemory(lpProcessPath, MAX_PATH);
DWORD nBufferLength = MAX_PATH;
is_success = GetCurrentDirectory(nBufferLength, lpProcessPath);
wstrCurrentProcessPath = lpProcessPath;
free(lpProcessPath);
lpProcessPath = NULL;
}

} while (false);

return is_success;
}

int main(int argc,char**argv)
{

std::wstring wstrCurrentProcessPath;
GetCurrentProcessDirectory(wstrCurrentProcessPath);
std::string strCurrentProcessPath = get_app_run_path();
SetCurrentDirectoryA(strCurrentProcessPath.c_str()  );
。。。

QApplication a(argc,argv);

QFile file(QString( "../qss/gocomUi.qss") );
file.open( QFile::ReadOnly );
QString styleSheet = QLatin1String( file.readAll() );
file.close();

a.setStyleSheet( styleSheet );

a.setStyle(new CProxyStyle);//去除焦点虚框

mainWindow w;
w.show();

。。。
int rt = a.exec();
return rt;
}


使用SetCurrentDirectoryA(strCurrentProcessPath.c_str() );就完成了程序所在目录的设置工作。

*注:

SetCurrentDirectory function

Changes the current directory for the current process.

参考:http://msdn.microsoft.com/en-us/library/windows/desktop/aa365530%28v=vs.85%29.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐