Path parameter reference of Qt learning summary (C fish)

1. Refer to the relative path:

For example: QCursor cursor(QPixmap("1.png"));

Problem: You will find that the reference fails, because the relative path starts from the current working directory to find the file. The current working directory can be obtained with the following functions:

bool QDir::setCurrent ( const QString & path ) [static]

Then you will find that the current working directory is C:\Users\Administrator, which does not match. So you can consider resetting the current working directory, and another problem, because Qt puts the project directory and the program directory in two places, for example:

Program directory: C:\Users\Administrator\Desktop\QT\build-Qt-Project-05-unknown-Debug
Project directory: C:\Users\Administrator\Desktop\QT\Qt-Project-05

As for the program directory, you can actually get it by calling the following function:

QString QCoreApplication::applicationDirPath () [static]

Well, now that we have the program directory, we can set the current working directory to the program directory, for example:

QDir::setCurrent(QCoreApplication::applicationDirPath());

2. Refer to the absolute path:

例如:
QCursor cursor(QPixmap("C://Users//Administrator//Desktop//QT//build-Qt-Project-05-unknown-Debug//debug//1.png"));

After testing, both '//' and '\' are valid.

3. Quoting the resource file:

When the resource file is added and the prefix is ​​set to '/'. Then it can be directly quoted, for example:

QCursor cursor(QPixmap(":/resource/1.png"));

Note the ':' character to be added before

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344460&siteId=291194637