Qt获取程序路径 之 QDir::currentPath与qApp->applicationDirPath
2018-01-11 09:50
2026 查看
Qt获取程序路径 之 QDircurrentPath与qApp-applicationDirPath
前言
解决办法
qApp-applicationDirPath
QDirCurrentDir
官方定义
该现象与程序的启动方式有关。使用绝对路径,相对路径和开机自启时获取的路径不一样。
(2)直接到vs2010工程相应的目录下(Debug或Release)去双击exe文件,QDir::currentPath()为“盘符:\工程名\工程名\Debug”,如:E:\vs2010\qt\QtProjects\tenNumAdd\tenNumAdd\Debug.
(3)通过cmd的命令启动相应的exe文件(如:start
E:\vs2010\qt\QtProjects\tenNumAdd\tenNumAdd\Debug***.exe),QDir::currentPath()为:C:\用户名。如果此时我所在的用户名为administrator,则QDir::currentPath()为C:\administrator。
由此可见,QDir::currentPath()在第三种情况下就不是我们想要的了。
( 参考:http://blog.csdn.net/bzhxuexi/article/details/38087119)
Returns the absolute path of the application’s current directory. The current directory is the last directory set with QDir::setCurrent() or, if that was never called, the directory at which this application was started at by the parent process.
这是一个静态方法,返回应用程序的当前路径(以绝对路径表示).当前目录是最近一次调用QDir::setCurrent()设置的值,如果彼函数未被调用过,当前目录即是被父进程开启时的目录
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
前言
解决办法
qApp-applicationDirPath
QDirCurrentDir
官方定义
Qt获取程序路径 之 QDir::currentPath()与qApp->applicationDirPath()
前言
一直使用QDir::currentPath()来获取程序所在绝对路径,根据路径在指定目录创建合适的文件夹。但是最近程序在开发板运行时文件夹创建的路径突然不对了。打印出来发现程序获取路径出错。该现象与程序的启动方式有关。使用绝对路径,相对路径和开机自启时获取的路径不一样。
解决办法
使用qApp->applicationDirPath() 替换 QDir::currentPath()
qApp->applicationDirPath()
无论你通过何种途径去运行文件,QAppllication::appllicationDirPath()的路径始终都是可执行文件所在的绝对路径。QDir::CurrentDir()
(1)在vs2010中,之间按F5调试,QDir::currentPath()为“盘符:\工程名\工程名”(也就是从cpp和.h文件所在的路径,如:E:\vs2010\qt\QtProjects\tenNumAdd\tenNumAdd).(2)直接到vs2010工程相应的目录下(Debug或Release)去双击exe文件,QDir::currentPath()为“盘符:\工程名\工程名\Debug”,如:E:\vs2010\qt\QtProjects\tenNumAdd\tenNumAdd\Debug.
(3)通过cmd的命令启动相应的exe文件(如:start
E:\vs2010\qt\QtProjects\tenNumAdd\tenNumAdd\Debug***.exe),QDir::currentPath()为:C:\用户名。如果此时我所在的用户名为administrator,则QDir::currentPath()为C:\administrator。
由此可见,QDir::currentPath()在第三种情况下就不是我们想要的了。
( 参考:http://blog.csdn.net/bzhxuexi/article/details/38087119)
官方定义
QString QDir::currentPath() [static]Returns the absolute path of the application’s current directory. The current directory is the last directory set with QDir::setCurrent() or, if that was never called, the directory at which this application was started at by the parent process.
这是一个静态方法,返回应用程序的当前路径(以绝对路径表示).当前目录是最近一次调用QDir::setCurrent()设置的值,如果彼函数未被调用过,当前目录即是被父进程开启时的目录
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
相关文章推荐
- QT中QDir::currentPath()和QAppllication::appllicationDirPath()用法区别
- QApplication::applicationDirPath()与QDir::currentPath()的区别
- 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别
- QDir::setCurrent(app.applicationDirPath());//
- 转载获取路径C#中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别
- 如何获取当前程序文件的路径CurrentPath
- 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别
- 关于getCurrentDir的问题及获取程序执行路径
- C++语言SetCurrentDir(ExtractFilePath(Application->ExeName))这句是什么意思
- Qt 获取程序运行路径
- 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory
- 关于apue第二章获取绝对路径长度程序path_alloc()分析
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- Current_Path 获取脚本所在路径(当前路径),取当前时间做文件名(uformat)
- How To Get Application Path,And How To Get The Path's Directory,File Name,And Type Info(如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息)
- c#,小发现,关于程序当前目录的问题,Environment.CurrentDirectory,Application.StartupPath;
- 解决Qt程序发布时中文乱码问题(通过QApplication.addLibraryPath加载QTextCodec插件)
- Qt 获取程序运行路径
- QT 获取可执行程序的路径
- 获取程序路径的方法.\\,GetCurrentDirectory与GetModuleFileName的区别