zoukankan      html  css  js  c++  java
  • Qt QDir::currentPath()和QAppllication::appllicationDirPath()用法区别

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

            【 调试环境:vs2010+ QT】

    1.QDir::currentPath()

             (1)在vs2010中,之间按F5调试,QDir::currentPath()为“盘符:工程名工程名”(也就是从***cpp和***文件所在的路径,如:E:vs2010qtQtProjects enNumAdd enNumAdd).

             (2)直接到vs2010工程相应的目录下(Debug或Release)去双击exe文件,QDir::currentPath()为“盘符:工程名工程名Debug”,如:E:vs2010qtQtProjects enNumAdd enNumAddDebug.

             (3)通过cmd的命令启动相应的exe文件(如:start  E:vs2010qtQtProjects enNumAdd enNumAddDebug***.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.

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

  • 相关阅读:
    洛谷题单 算法1-1 模拟和高精度
    第十一届蓝桥杯 b组
    jdk的安装、java环境配置
    哈夫曼树 java
    机器学习基石8-Noise and Error
    Java基础12-工具类;变长参数;IO
    机器学习基石7-The VC Dimension
    Java基础11-List;Set;Map
    Java基础10-集合
    机器学习基石6-泛化理论
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14307475.html
Copyright © 2011-2022 走看看