【Qt】Linux上设置自启动后qApp->applicationDirPath()的返回值问题
1、問題描述
開發(fā)程序過程中,使用qApp->applicationDirPath()返回程序所在目錄,一直沒問題。但是在設(shè)置自啟動后,qApp->applicationDirPath()返回的目錄不是程序所在目錄。
2、原因分析
查看qApp->applicationDirPath()官方手冊,發(fā)現(xiàn)針對Linux系統(tǒng),有如下警示說明:
Warning: On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
大意是:在Linux系統(tǒng)中,該函數(shù)嘗試獲取 /proc 中記錄的路徑;如果獲取失敗,在從argv[0]中獲取絕對路徑。這個函數(shù)假設(shè)程序沒有更改當(dāng)前目錄。
對比查看 /proc/[PID]/cwd 鏈接的路徑:開發(fā)時(shí),啟動程序在進(jìn)入程序所在目錄后啟動,此時(shí) /proc/[PID]/cwd 鏈接到程序所在目錄;在設(shè)置為自啟動后,啟動腳本直接在root根目錄下使用程序的絕對路徑啟動,此時(shí) /proc/[PID]/cwd 鏈接的是root根目錄。
3、解決方法
在設(shè)置啟動腳本時(shí),先 cd 到程序所在目錄,然后再執(zhí)行程序。
應(yīng)該還有其它方法,比如獲取程序絕對路徑qApp->applicationFilePath(),然后截取出程序路徑(這個方法沒有測試)。
如果我的能力只能讓我窮困潦倒,那窮困潦倒就是我的價(jià)值。
總結(jié)
以上是生活随笔為你收集整理的【Qt】Linux上设置自启动后qApp->applicationDirPath()的返回值问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【GStreamer】在x264enc中
- 下一篇: 【GStreamer】使用capsfil