can't compare lists of my struct
-
Hi all -
This is what I'm trying to do:
struct ValveConfiguration : public QObject { Q_OBJECT QUuid uuid; bool on; bool operator == (const ValveConfiguration &rhs) const { if (uuid == rhs.uuid && on == rhs.on) return true; return false; } bool operator != (const ValveConfiguration &rhs) const { return !(*this == rhs); } }; class Outcome : public QObject { Q_OBJECT QList<ValveConfiguration> m_valveConfigurationList; bool setValveConfigurationList(const QList<ValveConfiguration> &list) { if (m_valveConfigurationList != list) { // getting an error here. ....
I'm getting the following error:
outcome.cpp:55:34: Invalid operands to binary expression ('QList<ValveConfiguration>' and 'const QList<ValveConfiguration>') qlist.h:333:56: candidate template ignored: requirement 'std::conjunction_v<std::disjunction<std::is_base_of<QList<ValveConfiguration>, ValveConfiguration>, QTypeTraits::has_operator_equal<ValveConfiguration>>>' was not satisfied [with U = ValveConfiguration]
Can someone tell me what I'm doing wrong here? (The == operator fails, too.)
Thanks...
-
@mzimmers said in can't compare lists of my struct:
QList<ValveConfiguration>
You can't copy objects derived from QObject so you won't be able to add anything to this container.
-
Also, inside of
Q_OBJECT
macro, there is aprivate:
section, so anything you type afterQ_OBJECT
is private. You need to make it public:Q_OBJECT public: QUuid uuid; bool on; bool operator == (const ValveConfiguration &rhs) const { if (uuid == rhs.uuid && on == rhs.on) return true; return false; } bool operator != (const ValveConfiguration &rhs) const { return !(*this == rhs); }
-
@sierdzio said in can't compare lists of my struct:
Also, inside of Q_OBJECT macro, there is a private: section, so anything you type after Q_OBJECT is private
Since
Q_OBJECT
extends to#define Q_OBJECT \ public: \ QT_WARNING_PUSH \ Q_OBJECT_NO_OVERRIDE_WARNING \ static const QMetaObject staticMetaObject; \ virtual const QMetaObject *metaObject() const; \ virtual void *qt_metacast(const char *); \ virtual int qt_metacall(QMetaObject::Call, int, void **); \ QT_TR_FUNCTIONS \ private: \ Q_OBJECT_NO_ATTRIBUTES_WARNING \ Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \ QT_WARNING_POP \ struct QPrivateSignal { explicit QPrivateSignal() = default; }; \ QT_ANNOTATE_CLASS(qt_qobject, "")
and you usually use
Q_OBJECT
at the top of your class declaration.
If you would switch theprivate:
andpublic:
section from the#define Q_OBJECT
, which I initially thought would be a good idea, it would violate the C++ "standard" (common sense), because no access specifier in a C++ class meansprivate:
.Then, @mzimmers ' code would work as expected but you would ask yourself why :)
I doubt people look at theQ_OBJECT
code every day :) -
M mzimmers has marked this topic as solved on
-
@mzimmers Well, yes, because as @Christian-Ehrlicher said
You can't copy objects derived from QObject
So removing
QObject
from inherited class(es) will allow copying and your code to work. If you ever want to putQObject
in again you'll have work with a list of pointers,QList<QObject *>
. -
@mzimmers said in can't compare lists of my struct:
I removed the QObject derivation, and now it seems to work.
If you don't plan to ever use meta-object related stuff, then you really don't need a
QObject
derived class or theQ_OBJECT
macro (for metacalls / qt signals).