[SOLVED] Get all Objects with same class ?
-
[quote author="wally123" date="1422790846"]If i understnad you correctly, you recomment to use this QSet<T*>
( set of gathered objects created by gather method (?) )
as public and not to use JKSH's suggestion to make it private
and not avoid globally-accessible variables for now, because
to much efforts to implement it (overkill).
Is this correct ?
[/quote]Yes, that is what gyll recommended.Both approaches work.
- The global approach is the "fast" way, and saves you a few lines of code at the start
- The the private approach is the "safe" way, and reduces the risk of you making mistakes in the future.
As the program designer, you need to compare the costs and benefits of the different approaches and decide which one you want.
I leave you with this piece of wisdom from xkcd:
!http://imgs.xkcd.com/comics/good_code.png(Good Code)!
(source: http://xkcd.com/844/ ) -
nice !
now the Iterator ... :) -
this works to find by variable name:
@
QSet<KNhead*>::iterator it = qFind(KNhead::instances.begin(), KNhead::instances.end(), cell_2);@I introduced a public property to KNhead class as
@int testProp;@@KNhead *cell_2 = new KNhead;
cell_2->testPop = 222;
@Can i find/search the object in the entire QSet by this property ?
Means, find an object in KNhead::instances with testProp == 222 ?Of course i can go through each object an check each on the contents of the testProp,
i want to ask if there is a direct possibility by STL or Qt . -
If you want to search by one particular property, use a QMap instead of a QSet, and use the property as the map key.
@
// Declaring the map
QMap<int, KNhead*> instances;
@@
// Inserting into the map
KNhead::KNhead(int prop)
{
this->testProp = prop;
instances[prop] = this;// ASSUMPTION: The value of this property never changes
}
@@
// Retrieving from the map
KNhead *cell = instances[222];
@If you want to search by multiple properties, use an in-memory SQLite database.
-
just a quick correction to my last reply, dunno what was in my mind when i wrote it: i said that if declaring a static getter it should be declared as 'const', and this is not possible for static methods. however, i was wrong: in order to prevent using the getter method for changing the data value, it is the return value that should be declared as 'const', not the method itself.
So it is entirely possible to declare a clean getter method which returns a const reference:
@
// knhead.h
#include <unordered_set>class KNhead
{
static std::unordered_set<KNhead*> m_instances;
public:
KNhead();
~KNhead();
static const std::unordered_set<KNhead*>& instances();
};// knhead.cpp
#include "knhead.h"
#include <assert.h>std::unordered_set<KNhead*> KNhead::m_instances;
KNhead::KNhead() {
m_instances.insert(this);
}KNhead::~KNhead() {
assert(m_instances.count(this));
m_instances.erase(this);
}const std::unordered_set<KNhead*>& KNhead::instances() {
return m_instances;
}
@With the KNhead::instances() as defined above you're safe (the compiler won't let you do something like e.g. KNhead::instances().insert(x) , it will complain about you trying to change a const reference)
-
great ! thanks a lot
and it works !
-
Hello,
i have problem with the javastyle QMapiterator and need your help.
Seems i do not understand the concept at all.the STL style iterator works nice;
@ QMap<quint32, Indiv*>::iterator it4 = m_instances.find(this->devID);
qDebug() << it4.value();// Indiv(0x8b1c378, name = "cell_0")
// Indiv(0x8b23f08, name = "cell_1")
// Indiv(0x8ac1138, name = "cell_2")
// Indiv(0x8b2dbf8, name = "cell_3")
// Indiv(0x8af8870, name = "cell_4")
// Indiv(0x8b249f0, name = "cell_5")
// OK@I played the entire afternoon with the below code,
mostly the application crashes, sometimes i end up in a
neverending loop. No further ideas any more now ...// java iterator
@
QMapIterator<quint32, Indiv*> it5(m_instances);qDebug() << it5.hasNext(); // true ok while (it5.hasNext()) { qDebug() << it5.value(); it5.next(); }@
@#include <QDebug>
#include <QObject>
#include <QMap>
#include <QMapIterator>@thx wally
-
[quote author="wally123" date="1422983820"]
@
while (it5.hasNext()) {
qDebug() << it5.value();
it5.next();
}
@
[/quote]You need to call next() before value(). -
Yes, a fundamental issue :D
thank you
@
| item 0 | item 1 | item 2 | ... | item n |A B C
@
Does the it5.next() moves the iterator from initial
position A to B and then read item 0 ?I'm much too stupid for java stuff
-
[quote author="wally123" date="1422987332"]Yes, a fundamental issue :D
thank you [/quote]You're welcome :)
[quote author="wally123" date="1422987332"]
@
| item 0 | item 1 | item 2 | ... | item n |A B C
@
Does the it5.next() moves the iterator from initial
position A to B and then read item 0 ?I'm much too stupid for java stuff[/quote]I'm not sure (I don't use Java-style iterators myself), but yes that's what the "documentation":http://doc.qt.io/qt-5/qmapiterator.html says.
Qt's documentation is very comprehensive -- it's good to search it. If you use Google Chrome, "this tool":http://qt-project.org/forums/viewthread/36199 makes it easy to search.