- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 RegistrationList 类,它有一个函数,该函数应该遍历注册指针列表,然后返回传递给该函数的特定注册类型的总费用作为 QString。当我从我的 gui 实现类调用此函数时,它总是返回 0,即使我可以看到注册列表类中有注册指针。知道我做错了什么吗?我猜这与我对 QMetaObject 的使用有关但不确定。下面的第一个函数是 RegistrationList 函数,第二个是我的 gui 类中的插槽。
我的代码:
double RegistrationList::totalFees(QString t) {
double total = 0.00;
for (int i = 0; i < attendeeList.size(); ++i) {
if (attendeeList.at(i)->metaObject()->className() == t)
total += attendeeList.at(i)->calculateFee();
}
void MainWindow::totalFees_clicked() {
if (ui->rdbGuest->isChecked()) {
double total = m_rList.totalFees("GuestRegistration");
QMessageBox::information(this, "Total guest registration fees", QString("Total guest registration fees: R %1").arg(total), QMessageBox::Ok);
}
else if(ui->rdbStandard->isChecked()) {
double total = m_rList.totalFees("StandardRegistration");
QMessageBox::information(this, "Total standard registration fees", QString("Total standard registration fees: R%1").arg(total), QMessageBox::Ok);
}
else if (ui->rdbStudent->isChecked()) {
double total = m_rList.totalFees("StudentRegistration");
QMessageBox::information(this, "Total student registration fees", QString("Total student registration fees: R%1").arg(total), QMessageBox::Ok);
}
}
最佳答案
将 DEFINES += QT_NO_CAST_FROM_ASCII QT_NO_CAST_TO_ASCII
添加到您的项目文件,重新编译您的代码,并修复所有错误。
提示:您的 className
与 t
的比较并不像您认为的那样。您正在比较指针,而您应该在其中比较字符串。将测试重写为以下之一:
QString::fromLatin1(attendeeList.at(i)->metaObject()->className()) == t
,或
!strcmp(attendeeList.at(i)->metaObject()->className(), t.toLatin1())
。
此操作实际上应该是 Registration
类的成员(如果 attendeeList
包含 Registration*
类型的值):
class Registration : public QObject {
...
public:
bool is(const QString & className) const {
return QString::fromLatin1(metaObject()->className()) == t;
}
...
};
你的 totalFees
应该是一个 const
方法,然后你不需要所有的 at()
冗长: operator[]
会做你想做的事。您还应该通过引用传递不需要拷贝的字符串,而不是值。使用迭代器可以让您完全摆脱显式索引:
double RegistrationList::totalFees(const QString & t) const {
double total = 0.0;
for (auto it = attendeeList.begin(); it != attendeeList.end(); ++it)
if ((*it)->is(t)) total += (*it)->calculateFee();
return total;
}
如果您的编译器支持 range-for,您应该改用它。现在已经不是 00 年代了。
double RegistrationList::totalFees(const QString & t) const {
double total = 0.00;
for (auto attendee : attendeeList)
if (attendee->is(t)) total += attendee->calculateFee();
return total;
}
如果您愿意,您也可以使用 std::accumulate
(参见 this answer):
double RegistrationList::totalFees(const QString & t) const {
return std::accumulate(attendeeList.begin(), attendeeList.end(), 0.0,
[t](Registration* attendee) -> double {
return attendee->is(t) ? attendee->calculateFee() : 0.0;
});
}
最后,永远不要使用浮点类型来处理金钱。使用适当的类来包装整数类型以表示您希望处理的最低货币单位。
关于c++ - QMetaObject 在指针列表中查找类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32759197/
我有一个 RegistrationList 类,它有一个函数,该函数应该遍历注册指针列表,然后返回传递给该函数的特定注册类型的总费用作为 QString。当我从我的 gui 实现类调用此函数时,它总是
有没有人试过在 QObject 上调用重载运算符 indexOfMethod( "operator<<" ); 将返回 -1 .最好的办法是把你的 operator<<在基类中,使其成为非虚拟的,并让
我需要获取 QMetaObject 以动态创建对象实例。如果我知道这个对象,那么 QObject::metaObject() 就是我所需要的。如果我知道类,那么我可以使用 QObject::stati
我设置了一个QT菜单,它自动与 Action 函数on_actionOpen_triggered()连接。稍后我想将文件名字符串传递给该函数,以便在特殊情况下手动调用该函数。因此,我将函数签名更改为
我想使用QMetaObject::invokeMethod来调用一个对象的方法(稍后它会在另一个线程中运行,然后invokeMethod就派上用场了)。我在 Python 3.3 上使用 PySide
一套建立在 Qt 反射系统之上的数据模型似乎是一种自然的协同作用,但我还没有在 Qt 库本身或来自第 3 方的库中找到这样的东西。有谁知道这样的事情是否存在? 我正在寻找用于编辑和显示 QObject
我既不是 Qt 也不是 C++ 开发人员,但为了完成一项小任务,我必须注册一个 QMetaObject::Connection。 我的问题是,如何正确关闭连接 QMetaObject::Connect
我的类中有一个方法调用QMetaObject::invokeMethod。来自documentation我读到使用 Qt::DirectConnection 应该立即调用插槽。在我的代码中,我似乎体验
可能解决方案很简单,但是从对话框中删除 ButtonBox 后,出现以下错误: QMetaObject::connectSlotsByName: No matching signal for on_b
我有一个“RegistrationList”类,它保存了指向三种不同类型注册的指针列表。我有一个函数 calculateFees() 应该返回其中一种注册类型的总注册费用。我应该使用 QT 元对象系统
为了详细说明模棱两可的标题,我想大致了解元系统是如何动态工作的。 在静态上下文中使用插槽/属性访问器时,调用可能是内联的,毕竟如果有可能为什么不呢? 但是动态场景和查询索引呢?元对象是如何实现的?偏移
我目前正在移植 FitNesse 的 Slim 服务器,但我现在有点卡住了。我得到的是像这样的字符串: ("id_4", "call", "id", "setNumerator", "20") ("i
全网搜索,除this外,没有找到QMetaObject的引用。 .这个类还有其他方法。谁知道在哪里可以找到有关 QMetaObject 类的所有详细信息? 最佳答案 您可以通过下载和阅读来自 http
我正在用 Qt 做一个项目,其中 invoke 方法在单独的线程上运行并从主线程调用。如果我将 QByteArray 作为 const 传递,它会构建并运行。但是,如果我删除它构建的 const 但在
这是 QMetaObject::invokeMethod doesn't find the method 的跟进.调用没有参数的方法是有效的。但是将前面的问题扩展到带参数的方法让我再次失败。 请参阅以
我有一个父类(super class)Common,它继承自QObject。然后我得到了一个类Item,它继承自Common。 Common.h class Common : public QObje
我有以下代码: class A : public QObject { Q_OBJECT public: A() : QObject() { moveToThr
...从静态类和非主线程调用。 简而言之,我有一个类“sapp”,它有另一个静态类“tobj”作为静态成员。为了避免静态订单初始化失败,tobj 在 sapp 的方法中声明,该方法又返回 tobj 实
除了QMetaObject::invokeMethod是否有任何类型安全的方式来异步调用方法/插槽(也就是在 GUI 线程中排队执行)? QMetaObject::invokeMethod没有对函数名
我想创建类 A 的新实例,它继承了派生自 QObject 的基类。 如果 A 不在命名空间中,这可以正常工作,但如果 A 在命名空间中,则此调用返回空指针: QObject *o = metaObje
我是一名优秀的程序员,十分优秀!