QT中QDir::currentPath()和QAppllication::appllicationDirPath()用法区别

最近需要动态获取exe文件的路径信息,通过查找看到了QDir::currentPath(),但是,在后来调试程序的时候出现了错误,这个路径和我想象中的不一致。经过多次尝试和调试,总结如下:

        【 调试环境:vs2010+ QT】

1.QDir::currentPath()

         (1)在vs2010中,之间按F5调试,QDir::currentPath()为“盘符:\工程名\工程名”(也就是从***cpp和***文件所在的路径,如: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()在第三种情况下就不是我们想要的了。


2.QAppllication::appllicationDirPath()

     而无论你通过何种途径去运行exe文件,QAppllication::appllicationDirPath()的路径始终都是exe文件所在的绝对路径。

因此,如果想要获取固定的exe路径信息,还是建议使用QAppllication::appllicationDirPath()。

--------------------------------------------------------------------------------------

下面是官方定义:

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.

------------------------------------------------------------------------------

参考:区分并利用QDir::currentPath()和QApplication::applicationDirPath()


  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值