Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, April 2008
QMap<QPersistentModelIndex, ...> searching problem


Message 1 in thread

Hi !

I have a problem finding values in a QMap:

I have a QMap with the following type:

    QMap<QPersistentModelIndex, QGraphicsItem*>

Everything works fine until I do a changePersistentIndexList () in my
QAbstractItemModel from which the QPersistentModelIndex used as keys
in my QMap are issued.

Then sometimes:

     my_qmap.find (some_model_index) == my_qmap.end ()

return true although some_model_index is in the QMap.

So when this happen, I do the following:

     std::map<QPersistentModelIndex, QGraphicsItem*> test = my_qmap.toStdMap ();

and

      test.find (some_model_index) == test.end ()

return false, meaning some_model_index is found.

I also try finding the some_model_index index manually, by doing:

   foreach (QPersistentModelIndex p, my_qmap.keys ())
      if (p == some_model_index)
          qDebug () << "Hurray !"
                           << (p == some_model_index)
                           << (p < some_model_index)
                           << (some_model_index < p);

and I get "Hurray ! true false false" displayed.

My question is: what is this behaviour ? Why isn't the index found by the QMap
find () method while I can find it manually or in a std::map ?
If someone has any clue I would appreciate.

-- 
 [ signature omitted ] 

Message 2 in thread

Looks like a bug. Make a small compilable example that shows the problem?

Cheers,
Peter

> -----Ursprüngliche Nachricht-----
> Von: remi humbert [mailto:remi.humbert@xxxxxxxxx] 
> Gesendet: Mittwoch, 2. April 2008 13:32
> An: qt-interest@xxxxxxxxxxxxx
> Betreff: QMap<QPersistentModelIndex, ...> searching problem
> 
> Hi !
> 
> I have a problem finding values in a QMap:
> 
> I have a QMap with the following type:
> 
>     QMap<QPersistentModelIndex, QGraphicsItem*>
> 
> Everything works fine until I do a changePersistentIndexList () in my
> QAbstractItemModel from which the QPersistentModelIndex used as keys
> in my QMap are issued.
> 
> Then sometimes:
> 
>      my_qmap.find (some_model_index) == my_qmap.end ()
> 
> return true although some_model_index is in the QMap.
> 
> So when this happen, I do the following:
> 
>      std::map<QPersistentModelIndex, QGraphicsItem*> test = 
> my_qmap.toStdMap ();
> 
> and
> 
>       test.find (some_model_index) == test.end ()
> 
> return false, meaning some_model_index is found.
> 
> I also try finding the some_model_index index manually, by doing:
> 
>    foreach (QPersistentModelIndex p, my_qmap.keys ())
>       if (p == some_model_index)
>           qDebug () << "Hurray !"
>                            << (p == some_model_index)
>                            << (p < some_model_index)
>                            << (some_model_index < p);
> 
> and I get "Hurray ! true false false" displayed.
> 
> My question is: what is this behaviour ? Why isn't the index 
> found by the QMap
> find () method while I can find it manually or in a std::map ?
> If someone has any clue I would appreciate.
> 
> -- 
> Humbert Remi
> 
> --
> 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 ] 

Message 3 in thread

Ok the problem was:

QMap (or QHash, I have also tried with a QHash instead of QMap and got
the same problem)
sort it's keys.

I use QPersistentModelIndex as keys. When I sort my model, I do
changePersistentIndexList () which
reorder the QPersistentModelIndexes. But QMap is unaware of that and
start having a wrong behaviour.
(it cant find the keys that have been swapped)

The only solution I found is to recreate a temporary map tmp_map from
my_qmap, then do
my_qmap.clear ();
my_qmap = tmp_map;


On Wed, Apr 2, 2008 at 1:49 PM, Peter Prade <prade@xxxxxxxxxxx> wrote:
> Looks like a bug. Make a small compilable example that shows the problem?
>
>  Cheers,
>  Peter
>
>  > -----Ursprüngliche Nachricht-----
>  > Von: remi humbert [mailto:remi.humbert@xxxxxxxxx]
>  > Gesendet: Mittwoch, 2. April 2008 13:32
>  > An: qt-interest@xxxxxxxxxxxxx
>  > Betreff: QMap<QPersistentModelIndex, ...> searching problem
>
>
> >
>  > Hi !
>  >
>  > I have a problem finding values in a QMap:
>  >
>  > I have a QMap with the following type:
>  >
>  >     QMap<QPersistentModelIndex, QGraphicsItem*>
>  >
>  > Everything works fine until I do a changePersistentIndexList () in my
>  > QAbstractItemModel from which the QPersistentModelIndex used as keys
>  > in my QMap are issued.
>  >
>  > Then sometimes:
>  >
>  >      my_qmap.find (some_model_index) == my_qmap.end ()
>  >
>  > return true although some_model_index is in the QMap.
>  >
>  > So when this happen, I do the following:
>  >
>  >      std::map<QPersistentModelIndex, QGraphicsItem*> test =
>  > my_qmap.toStdMap ();
>  >
>  > and
>  >
>  >       test.find (some_model_index) == test.end ()
>  >
>  > return false, meaning some_model_index is found.
>  >
>  > I also try finding the some_model_index index manually, by doing:
>  >
>  >    foreach (QPersistentModelIndex p, my_qmap.keys ())
>  >       if (p == some_model_index)
>  >           qDebug () << "Hurray !"
>  >                            << (p == some_model_index)
>  >                            << (p < some_model_index)
>  >                            << (some_model_index < p);
>  >
>  > and I get "Hurray ! true false false" displayed.
>  >
>  > My question is: what is this behaviour ? Why isn't the index
>  > found by the QMap
>  > find () method while I can find it manually or in a std::map ?
>  > If someone has any clue I would appreciate.
>  >
>  > --
>  > Humbert Remi
>  >
>  > --
>  > 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/
>  >
>  >
>
>  --
>  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 ] 

Message 4 in thread

I see, you're changing the order of the keys, but QMap can't handle this.

Maybe you could use a QHash (which isn't ordered) instead, this should work, if you can come up with a qHash() implementation that doesn't result in different hash values when you call changePersistentIndexList().

Cheers,
Peter

> -----Ursprüngliche Nachricht-----
> Von: remi humbert [mailto:remi.humbert@xxxxxxxxx] 
> Gesendet: Mittwoch, 2. April 2008 15:47
> An: Peter Prade
> Cc: qt-interest@xxxxxxxxxxxxx
> Betreff: Re: QMap<QPersistentModelIndex, ...> searching problem
> 
> Ok the problem was:
> 
> QMap (or QHash, I have also tried with a QHash instead of QMap and got
> the same problem)
> sort it's keys.
> 
> I use QPersistentModelIndex as keys. When I sort my model, I do
> changePersistentIndexList () which
> reorder the QPersistentModelIndexes. But QMap is unaware of that and
> start having a wrong behaviour.
> (it cant find the keys that have been swapped)
> 
> The only solution I found is to recreate a temporary map tmp_map from
> my_qmap, then do
> my_qmap.clear ();
> my_qmap = tmp_map;
> 
> 
> On Wed, Apr 2, 2008 at 1:49 PM, Peter Prade <prade@xxxxxxxxxxx> wrote:
> > Looks like a bug. Make a small compilable example that 
> shows the problem?
> >
> >  Cheers,
> >  Peter
> >
> >  > -----Ursprüngliche Nachricht-----
> >  > Von: remi humbert [mailto:remi.humbert@xxxxxxxxx]
> >  > Gesendet: Mittwoch, 2. April 2008 13:32
> >  > An: qt-interest@xxxxxxxxxxxxx
> >  > Betreff: QMap<QPersistentModelIndex, ...> searching problem
> >
> >
> > >
> >  > Hi !
> >  >
> >  > I have a problem finding values in a QMap:
> >  >
> >  > I have a QMap with the following type:
> >  >
> >  >     QMap<QPersistentModelIndex, QGraphicsItem*>
> >  >
> >  > Everything works fine until I do a 
> changePersistentIndexList () in my
> >  > QAbstractItemModel from which the QPersistentModelIndex 
> used as keys
> >  > in my QMap are issued.
> >  >
> >  > Then sometimes:
> >  >
> >  >      my_qmap.find (some_model_index) == my_qmap.end ()
> >  >
> >  > return true although some_model_index is in the QMap.
> >  >
> >  > So when this happen, I do the following:
> >  >
> >  >      std::map<QPersistentModelIndex, QGraphicsItem*> test =
> >  > my_qmap.toStdMap ();
> >  >
> >  > and
> >  >
> >  >       test.find (some_model_index) == test.end ()
> >  >
> >  > return false, meaning some_model_index is found.
> >  >
> >  > I also try finding the some_model_index index manually, by doing:
> >  >
> >  >    foreach (QPersistentModelIndex p, my_qmap.keys ())
> >  >       if (p == some_model_index)
> >  >           qDebug () << "Hurray !"
> >  >                            << (p == some_model_index)
> >  >                            << (p < some_model_index)
> >  >                            << (some_model_index < p);
> >  >
> >  > and I get "Hurray ! true false false" displayed.
> >  >
> >  > My question is: what is this behaviour ? Why isn't the index
> >  > found by the QMap
> >  > find () method while I can find it manually or in a std::map ?
> >  > If someone has any clue I would appreciate.
> >  >
> >  > --
> >  > Humbert Remi

--
 [ signature omitted ]