C++ QT创建者错误(运算符不匹配=)

C++ QT创建者错误(运算符不匹配=),c++,qt,qtcore,qdir,qstandardpaths,C++,Qt,Qtcore,Qdir,Qstandardpaths,每次生成代码时,我都会收到此错误。我正在使用QtCreator3.1,(5.2.1版本) 错误:“运算符+”不匹配(操作数类型为“QStringList”和“const char[2]”) 下面是一段代码,希望能有所帮助(asterix行是突出显示错误的地方) QStandardPath::standardLocations返回QStringList。您应该使用QStringList::join或QStringList::at或使用foreach。Mb您想做其他事情,但我不知道是什么,因为变量M

每次生成代码时,我都会收到此错误。我正在使用QtCreator3.1,(5.2.1版本)

错误:“运算符+”不匹配(操作数类型为“QStringList”和“const char[2]”)

下面是一段代码,希望能有所帮助(asterix行是突出显示错误的地方)


QStandardPath::standardLocations返回QStringList。您应该使用QStringList::join或QStringList::at或使用foreach。Mb您想做其他事情,但我不知道是什么,因为变量MainPath非常神秘=)

问题是您试图将QStringList与qstring连接起来,因为

QStandardPaths::standardLocations(QStandardPaths::HomeLocation)
返回一个
QStringList

您需要对希望重用的元素进行gt,例如使用
.first()
方法。你可以这样写:

MainPath=QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()+“/”+a.applicationName()+“/Data”

请注意,我只是在应用程序名称和“数据”之间添加了一个缺少的“/”,因为我认为这样使用更符合逻辑,但如果您愿意,可以随意拒绝该编辑

但是,由于您似乎对数据目录位置感兴趣,我建议使用
QStandardPaths
中的专用枚举:

或者干脆用:

DataLocation 9返回可以存储持久应用程序数据的目录位置。QCoreApplication::organizationName和QCoreApplication::applicationName追加到为GenericDataLocation返回的目录位置

你可以这样写:

QDir Path(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first());
事实上,如果您希望避免调用
.first()
,可能可以使用以下方法:

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

出于好奇,这也可能是另一种选择:

详情如下:

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

如果这还不够,甚至还有:


你真是太棒了,拉兹洛,我今天要做实验,让你知道我的结果。谢谢你的精彩聪明的回答!
QDir Path(QCoreApplication::applicationDirPath + "/Data");