例如: QCursor cursor(QPixmap("1.png"));app
問題:會發現引用失敗,這是由於相對路徑都是從當前工做目錄開始找起文件的。能夠經過如下函數獲取當前工做目錄:ide
bool QDir::setCurrent ( const QString & path ) [static]函數
而後會發現當前工做目錄是C:\Users\Administrator,與之不符。因此能夠考慮將當前工做目錄從新設置,而另外一個問題,由於Qt是將工程目錄和程序目錄放在兩個地方,例如:測試
程序目錄: C:\Users\Administrator\Desktop\QT\build-Qt-Project-05-unknown-Debug
工程目錄: C:\Users\Administrator\Desktop\QT\Qt-Project-05ui
至於程序目錄,其實能夠經過調用如下函數來獲取:debug
QString QCoreApplication::applicationDirPath () [static]資源
好了,如今獲得了程序目錄,咱們就能夠將當前工做目錄設置爲程序目錄,例如:it
QDir::setCurrent(QCoreApplication::applicationDirPath());io
例如:
QCursor cursor(QPixmap("C://Users//Administrator//Desktop//QT//build-Qt-Project-05-unknown-Debug//debug//1.png"));class
通過測試,‘//’和‘\’,都有效。
當添加了資源文件後,而且前綴設置爲‘/’。那麼就能夠直接引用了,例如:
QCursor cursor(QPixmap(":/resource/1.png"));
注意前面要添加‘:’字符