gpt4 book ai didi

c++ - 奇怪的 QMetaObject 错误

转载 作者:行者123 更新时间:2023-11-28 02:20:46 37 4
gpt4 key购买 nike

我有一个“RegistrationList”类,它保存了指向三种不同类型注册的指针列表。我有一个函数 calculateFees() 应该返回其中一种注册类型的总注册费用。我应该使用 QT 元对象系统来检查特定类型注册实例的列表,但是当我运行该程序时,出现以下错误:

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-a2-q1-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\moc_registrationlist.cpp:63: error: 'staticMetaObject' 不是 'QList' 的成员 { &QList::staticMetaObject, qt_meta_stringdata_RegistrationList.data, ^

我的 calculateFees 函数代码:

double RegistrationList::totalFees(QString t) {
double total = 0.00;
for (int i = 0; i <= this->size(); ++i) {
if (attendeeList.at(i)->metaObject()->className() == t)
total += this->at(i)->calculateFee();
}
return total;
}

最佳答案

QList 不是从 QObject 派生的。

“错误:‘staticMetaObject’不是成员”的第一个谷歌结果: Link

What this is saying is that QTreeWidgetItem does not inherit from QObject, meaning that your own, singly-inherited class also does not inherit from QObject. Inheriting from QObject is one of the prerequisites to using the Q_OBJECT macro, which, if you're anything like me, you automatically insert into any Qt GUI related class.

If you're not using any of the meta object stuff in your subclass, such as signals/slots or properties, just take out the Q_OBJECT macro. If you need to use signals and slots, you'll need to make your subclass multiply-inherit from QObject as well. If you take this route, remember that "Multiple Inheritance Requires QObject to Be First", otherwise you'll get either the same error as above, or something along the lines of "YourClass inherits from two QObject subclasses" from the moc.

QList 替换 QTreeWidgetItem 就可以了。

关于c++ - 奇怪的 QMetaObject 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32618571/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com