Qt-interest Archive, August 2007
Comparing lists of pointers
Message 1 in thread
Hi,
On Qt4, I have a list of pointers to QObjects that I want to compare
with another one. The comparison test should not be based on the
identities of the objects (ie their pointer address in this case) but on
their equalities (ie their values). In the STL, we can provide to the
equal algorithm our own comparison function in which we dereference the
pointers being compared to test their values. But in QVector, I don't
see any way to do that. The only solution was to implement ourselves the
operator==. Do you know a way to do that ?
Here is a code example where instances of QString should be replaced
with a model class derived from QObject:
#include <QtCore>
#include <iostream>
int main(int argc, char *argv[])
{
QList<QString*> list1;
list1.append(new QString("5"));
list1.append(new QString("2"));
QList<QString*> list2;
list2.append(new QString("5"));
list2.append(new QString("2"));
if (list1 == list2) // I would have imagined a list1.equal(list2,
Qt::PointerComparison)
std::cout << "OK" << endl;
else
std::cout << "NOK" << endl;
return 0;
}
Thank you!
Olivier
--
[ signature omitted ]
Message 2 in thread
On mandag den 13. August 2007, Olivier Guérin wrote:
> Hi,
>
> On Qt4, I have a list of pointers to QObjects that I want to compare
> with another one. The comparison test should not be based on the
> identities of the objects (ie their pointer address in this case) but on
> their equalities (ie their values). In the STL, we can provide to the
> equal algorithm our own comparison function in which we dereference the
> pointers being compared to test their values. But in QVector, I don't
> see any way to do that. The only solution was to implement ourselves the
> operator==. Do you know a way to do that ?
If your stl code works, why not just use that? The Qt containers should be
usable in the stl algorithms and functions. And if you can't get that
working, there's a toStdVector in QVector.
Bo.
--
[ signature omitted ]
Message 3 in thread
Hi,
Implement helper function like:
bool operator==( const QVector<MyClass> &roQV1, const QVector<MyClass>
&roQV2) {
...
}
or follow implementation line used in STL (in case above operator==(...)
leads to ambiguities):
bool cmp( const QVector<MyClass> &roQV1, const QVector<MyClass> &roQV2) {
}
Samvel.
Bo Thorsen wrote:
> On mandag den 13. August 2007, Olivier Guérin wrote:
>
>> Hi,
>>
>> On Qt4, I have a list of pointers to QObjects that I want to compare
>> with another one. The comparison test should not be based on the
>> identities of the objects (ie their pointer address in this case) but on
>> their equalities (ie their values). In the STL, we can provide to the
>> equal algorithm our own comparison function in which we dereference the
>> pointers being compared to test their values. But in QVector, I don't
>> see any way to do that. The only solution was to implement ourselves the
>> operator==. Do you know a way to do that ?
>>
>
> If your stl code works, why not just use that? The Qt containers should be
> usable in the stl algorithms and functions. And if you can't get that
> working, there's a toStdVector in QVector.
>
> Bo.
>
>
Message 4 in thread
Bo Thorsen a écrit :
> If your stl code works, why not just use that? The Qt containers should be
> usable in the stl algorithms and functions. And if you can't get that
> working, there's a toStdVector in QVector.
OK, thank you, I will use the STL comparison algorithm with a comparison
predicate of my own.
--
[ signature omitted ]
Message 5 in thread
As long as the individual object comparison functions are public, you should be able to write a simple iterate and compare of the two vectors..
Also, I don't know how precise your comparison needs to be.. Ie, if the objects are the same, but out of order... etc etc.. But something like the following should work.
template < typename T >
bool operator==( const QVector< T > & vOne, const QVector< T > & vTwo )
{
}
Scott
> -----Original Message-----
> From: Olivier Guérin [mailto:olivierguerin@xxxxxxxxxx]
> Sent: Monday, August 13, 2007 12:29 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Comparing lists of pointers
>
> Hi,
>
> On Qt4, I have a list of pointers to QObjects that I want to compare
> with another one. The comparison test should not be based on the
> identities of the objects (ie their pointer address in this case) but on
> their equalities (ie their values). In the STL, we can provide to the
> equal algorithm our own comparison function in which we dereference the
> pointers being compared to test their values. But in QVector, I don't
> see any way to do that. The only solution was to implement ourselves the
> operator==. Do you know a way to do that ?
>
> Here is a code example where instances of QString should be replaced
> with a model class derived from QObject:
>
> #include <QtCore>
> #include <iostream>
>
> int main(int argc, char *argv[])
> {
> QList<QString*> list1;
> list1.append(new QString("5"));
> list1.append(new QString("2"));
> QList<QString*> list2;
> list2.append(new QString("5"));
> list2.append(new QString("2"));
> if (list1 == list2) // I would have imagined a list1.equal(list2,
> Qt::PointerComparison)
> std::cout << "OK" << endl;
> else
> std::cout << "NOK" << endl;
> return 0;
> }
>
> Thank you!
>
> Olivier
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]