c++qtqtcoreqdirqstandardpaths

QT Creator Error (no match for operator =)


I'm receiving this error everytime I build my code. I'm using QT Creator 3.1, (5.2.1 build)

error: no match for 'operator+' (operand types are 'QStringList' and 'const char [2]')

Here is a snippet of code, hope it can help (the asterix line is where the error is highlighted)

int main(int argc, char *argv[])
{
Shutdown = false;

QApplication a(argc, argv);
a.setApplicationName("aQtLow");

//Create default data paths if they don't exist
QDir Path;
**MainPath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation) + "/" + a.applicationName() + "Data";**
Path.setPath(MainPath);

Solution

  • The problem is that you are trying to concatenate a QStringList with QStrings since

    QStandardPaths::standardLocations(QStandardPaths::HomeLocation)
    

    returns a QStringList.

    You would need to gt the element you wish to reuse, e.g. using the .first() method. You could write this:

    MainPath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first() + "/" + a.applicationName() + "/Data";

    Note that I just added a missing "/" between the application name and the "Data" because I think that would be more logical to use, but feel free to reject that edit if you wish.

    But since you seem to be interested in the data directory location, I would suggest to use the dedicated enum from QStandardPaths:

    or it would be even better to just use:

    QStandardPaths::DataLocation 9 Returns a directory location where persistent application data can be stored. QCoreApplication::organizationName and QCoreApplication::applicationName are appended to the directory location returned for GenericDataLocation.

    You could write this then:

    QDir Path(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first());
    

    In fact, if you wish to avoid the .first() call, you could probably use the writableLocation() method as follows:

    QDir Path(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
    

    =====================================================

    Out of curiosity, this could also be an alternative:

    QString QDir::homePath () [static]

    or

    QDir QDir::home () [static]

    as follows:

    QDir Path = QDir::home();
    Path.cd(a.applicationName() + "Data");
    

    or

    QDir Path(QDir::homePath() + "/" + a.applicationName() + "/Data");
    

    If that is not enough, there is even one more alternative:

    QDir Path(QCoreApplication::applicationDirPath + "/Data");