QT创建者错误(与操作员不匹配=)

QT Creator Error (no match for operator =)

本文关键字:不匹配 操作员 创建者 错误 QT      更新时间:2023-10-16

每次构建代码时,我都会收到这个错误。我使用的是QT Creator 3.1(5.2.1版本)

错误:"operator+"不匹配(操作数类型为"QStringList"answers"const-char[2]")

这里有一段代码,希望它能有所帮助(星号行是错误突出显示的地方)

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);

问题是,自以来,您正试图将QStringList与QStrings连接起来

QStandardPaths::standardLocations(QStandardPaths::HomeLocation)

返回CCD_ 1。

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

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

请注意,我只是在应用程序名称和"Data"之间添加了一个缺失的"/",因为我认为使用它更合乎逻辑,但如果您愿意,可以随意拒绝该编辑。

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

或者只使用会更好

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

你可以这样写:

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

事实上,如果您希望避免.first()调用,您可能会使用writableLocation()方法,如下所示:

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

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

出于好奇,这也可能是一个替代方案:

QString QDir::homePath()[static]

QDirQDir::主页()〔静态〕

如下所示:

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

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

如果这还不够的话,还有一个选择:

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

QStandardPaths::standardLocations返回QStringList。您应该在或使用foreach处使用QStringList::join或QSStringList::。Mb你想做其他事情,但我不知道是什么,因为变量MainPath太神秘了=)