Results 1 to 1 of 1

Thread: QItemSelectionModel synchronization problem - SOLVED

  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QItemSelectionModel synchronization problem - SOLVED

    I have a tree-structured set of data, for which I have created a QAbstractItemModel to display it in a QTreeView. At the same time, I implemented a custom QSortFilterProxyModel that flattens the tree-based QAbstractItemModel into a table (each leaf in the tree is a row in the table, and the higher level tree items repeat for their leaf rows). This works fine.

    Most of the data in the structure is numeric, and I have implemented a set of QGraphicsView-based scatterplots, pie charts, bar charts, etc. that will display selected columns from the table. These are all derived from QAbstractItemView, so they use QAbstractItemModel instances to source their data. I've done this by creating another proxy model that can be configured to pull x, y, and z coordinate data out of the table model by telling the proxy which column and role to use for which coordinate.

    All of this now works great. When the tree-structured data is updated, the tree view, table view, and all the plots update just fine.

    I now am trying to get selection from the tree, table, and plots synchronized. I want the same items to show as selected whether they appear in the tree, table, or plot views, and I want to be able to select an item from any of these. Because of all the proxy models, I can't just create a custom QItemSelectionModel and install it everywhere, because Qt then complains that the models used are different for different views. That's true - except for the tree, everything else uses proxies, sometimes several layers of them so model indexes are being mapped to and from source models all over the place.

    So, I am at the point where I am handling selection of QGraphicsItem instances from the plots and mapping them to their underlying QModelIndex instances. I then use the default QItemSelectionModel for each view and call the select() slot. This results in the QItemSelectionModel::selectionChanged() signal being fired.

    Here's where the trouble starts. I want to handle that signal in the other views to synchronize the selections there. QAbstractItemView, the base class for all of my plot views, doesn't have any way to set the selection except through the selection model. But if I do that, then I get another selectionChanged() signal from that instance and I end up with this recursive mess of select() and selectionChanged() calls.

    As I write this, I am starting to think that the way to solve this is to implement my own QItemSelectionModel instance and install it on each view. This class would probably trap the selectionChanged() signal and map it into something else so I could prevent recursion, or maybe I would reimplement the select() slot so it doesn't fire the signal in the first place.

    Does that make sense? If not, can someone please suggest an alternative?

    Thanks in advance for any help / advice.

    Edit ---

    Duh, sometimes the simplest solutions get lost in the confusion of over-thinking the problem. I've solved the recursion problem by setting a boolean flag in the slot that handles the selectionChanged() signals from the various views. When the slot is first entered, I check the flag, and if it is not set, I set the flag, update the rest of the views, then clear the flag. When one of the selection models in the other views issues its signal, it is blocked from recursion in the slot by the flag. So all of the plots are now in sync, and I need to get the tree and table views synced as well.
    Last edited by d_stranz; 17th May 2014 at 00:06.

Similar Threads

  1. QItemSelectionModel and QTreeView
    By rrafluap in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2010, 20:49
  2. thread synchronization
    By freekill in forum Qt Programming
    Replies: 10
    Last Post: 6th January 2010, 18:29
  3. Thread Synchronization Problem
    By sijithjames in forum Qt Programming
    Replies: 13
    Last Post: 13th November 2009, 15:53
  4. audi/video synchronization problem in qtopia
    By Savita in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 19th February 2008, 07:10
  5. QSplitter synchronization
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2006, 18:22

Tags for this Thread

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.