Results 1 to 4 of 4

Thread: selectionModel() strange behavior

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default selectionModel() strange behavior

    Hi everyone,

    I got somehting really weird and I wonder if you guys could help me :

    I have a QListView that displays my items, and I want to get the indexes from any Extended Selection.
    So I get the signal like this :

    connect(centralView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelect ion)), this, SLOT(selectInTrace(QItemSelection,QItemSelection)) );

    But, the problem is : no matter how many item I select in my centralView, only the last one is emitted by the SIGNAL selectionChanged. At first, I thought the selectionModel wasn't selecting all the wanted items, but, in my SLOT, I tried to use centralView->selectionModel()->selection() (instead of the first QItemSelection related to the selected items) and it effectively returns all the desired items...

    Of course, I have set the central view selection mode as :

    centralView->setSelectionMode(QAbstractItemView::ExtendedSelec tion);

    Which confirms that the problem does not come from the selectionModel itself, but rather from the SIGNAL (at least, that is the only logical cause I have figured out).

    The problem is not a real problem itself (my app have a decent behavior by using the centralView->selectionModel()->selection() ) but if someone could tell me how to fix this little bug, I will be quite thankful, for I feel like my code is a bit trashy.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: selectionModel() strange behavior

    Hmm.

    Maybe the signal doesn't transport the new and the old state but the two changes, i.e. the things that are now additionally part of the selection (first argument) and the things that are no longer part of the selection (second argument).

    You could try using the select() method to select more than one index in one go and check if all of them are in the first argument.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: selectionModel() strange behavior

    Hi anda, and thank you for your answer.

    I'm not really sure I completely understood what you told me to do, but, in fact, I use the select() method.
    Let me explain :

    I have 2 QListViews, each one linked to 2 different proxy models, both of them having the same sourceModel.
    When I select items on the first view, I want my second view to select the same item and scroll to it.
    On the other hand, I have a button that, when activated, display only the selected items in my second view. The strange part is, when i check -> uncheck this button, the selection of the first view is now fully selected in the second (while it doesn't if I do nothing but select in the first view).

    I hope this will help you a bit more

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: selectionModel() strange behavior

    The usual approach to ensuring two views show the same selection is to use the same selection model in both views. This, however, requires that the views have the same source; not a situation you have because of the proxy models.

    The signal carries only the differences between the previous selection and the new selection: a set of newly selected items, and a set of newly unselected items. Your slot needs to map the deleted item indexes to indexes in the second model and unselect them from the existing selection on the second view. It should then repeat the activity for the newly selected items. To map the indexes will require the use of the proxy model mapToSource and mapFromSource functions (I think that is the names) because the selections will be proxy indexes and not source indexes.

Similar Threads

  1. strange behavior of qt 5.4
    By gisac in forum Qt Programming
    Replies: 4
    Last Post: 26th December 2015, 21:32
  2. Qt process strange behavior
    By rspock in forum Newbie
    Replies: 5
    Last Post: 15th March 2013, 17:11
  3. strange behavior of paintEvent
    By hashb in forum Qt Programming
    Replies: 3
    Last Post: 30th August 2010, 07:48
  4. Qt Creator Strange behavior of GDB in Creator 1.3
    By rayjc in forum Qt Tools
    Replies: 1
    Last Post: 11th March 2010, 21:42
  5. Strange behavior of QSyntaxHighlighter in Qt3
    By lorebett in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2009, 14:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.