Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sync selectionModel between two views where one view has a transposed Model of the other
Forum Update on Monday, May 27th 2025

Sync selectionModel between two views where one view has a transposed Model of the other

Scheduled Pinned Locked Moved Unsolved General and Desktop
model-viewselectionmodelbeginner
9 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    firen
    wrote on last edited by
    #1

    Hello,

    I have two views. The first one uses the 'original' model and the second one uses the transposed model. See the general structure below:

    original_Model = new QAbstractTableModel(this); 
    transposed_Model = new QTransposeProxyModel(this); 
    transposed_Model->setSourceModel(original_Model);
    
    original_View= new QTableView(this);
    original_View->setModel(original_Model);
    
    transposed_View = new QTableView(this);
    transposed_View ->setModel(transposed_Model);
    
    m_viewTransposed->setSelectionModel(m_viewOriginal->selectionModel());
    

    As far as I know, i need the SAME model in the views, that
    m_viewTransposed->setSelectionModel(m_viewOriginal->selectionModel());
    would work. My question now is what is the best approach to sync the two selectionModels betweeen the two views. It cant be that difficult, because I only need to switch the columns and rows. Do I have to rewrite QItemSelectionModel? (A general hint would be enough)

    Thank you in advance!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Easy solution: KLinkItemSelectionModel https://api.kde.org/frameworks/kitemmodels/html/classKLinkItemSelectionModel.html

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      JonBJ 1 Reply Last reply
      2
      • VRoninV VRonin

        Easy solution: KLinkItemSelectionModel https://api.kde.org/frameworks/kitemmodels/html/classKLinkItemSelectionModel.html

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @VRonin
        Purely because I am interested in this.

        I looked at that link's documentation, but not its source code. I do note that it explains:

        Although multiple views can share the same QItemSelectionModel, the views then need to have the same source model.

        which clarifies why distinct QItemSelectionModels will be needed.

        If the OP does not want to import KDE code, for whatever reason, in the specific case of QTransposeProxyModel is it indeed correct that all he has to do is "I only need to switch the columns and rows.", which might be simple to write in a couple of lines himself, rather than the generic solution obviously offered by KDE?

        F 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          It's a bit of a pain in the 🍑 as you'd also have to handle changes in the source model (see QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved for example). Not worth the effort, if you ask me, when you can just snag high quality code (Stephen Kelly original author and the current maintainer is David Faure that also maintains the model/views module of Qt) and be done with 1 line

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • JonBJ JonB

            @VRonin
            Purely because I am interested in this.

            I looked at that link's documentation, but not its source code. I do note that it explains:

            Although multiple views can share the same QItemSelectionModel, the views then need to have the same source model.

            which clarifies why distinct QItemSelectionModels will be needed.

            If the OP does not want to import KDE code, for whatever reason, in the specific case of QTransposeProxyModel is it indeed correct that all he has to do is "I only need to switch the columns and rows.", which might be simple to write in a couple of lines himself, rather than the generic solution obviously offered by KDE?

            F Offline
            F Offline
            firen
            wrote on last edited by firen
            #5

            Hello @JonB,

            yes I dont want to import KDE code (only pure C++ and Qt).
            Could you give me a hint what exactly I have to rewrite?

            I tested for example:

            connect(original_View->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
              transposed_View, SLOT(onSelectionChanged(const QModelIndex &, const QModelIndex &)));
            

            And in on SelectionChanged I use something like:

            void CTableWidgetView::onSelectionChanged(const QModelIndex & originalIndexSelected, const QModelIndex & originalIndexDeselected)
            {
            
              selectionModel()->setCurrentIndex(model()->index(originalIndexSelected.column(), 
                originalIndexSelected.row()), QItemSelectionModel::Select);
            
              
              selectionModel()->setCurrentIndex(model()->index(originalIndexDeselected.column(),
                originalIndexDeselected.row()), QItemSelectionModel::Deselect);
            
            }
            

            But this works only in one direction and only if i select only ONE cell.

            Thank you!

            VRoninV JonBJ 2 Replies Last reply
            0
            • F firen

              Hello @JonB,

              yes I dont want to import KDE code (only pure C++ and Qt).
              Could you give me a hint what exactly I have to rewrite?

              I tested for example:

              connect(original_View->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
                transposed_View, SLOT(onSelectionChanged(const QModelIndex &, const QModelIndex &)));
              

              And in on SelectionChanged I use something like:

              void CTableWidgetView::onSelectionChanged(const QModelIndex & originalIndexSelected, const QModelIndex & originalIndexDeselected)
              {
              
                selectionModel()->setCurrentIndex(model()->index(originalIndexSelected.column(), 
                  originalIndexSelected.row()), QItemSelectionModel::Select);
              
                
                selectionModel()->setCurrentIndex(model()->index(originalIndexDeselected.column(),
                  originalIndexDeselected.row()), QItemSelectionModel::Deselect);
              
              }
              

              But this works only in one direction and only if i select only ONE cell.

              Thank you!

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @firen said in Sync selectionModel between two views where one view has a transposed Model of the other:

              I dont want to import KDE code

              You really prefer code posted by randos on a forum to just using classes tried and tested by a major organisation?

              only pure C++ and Qt

              KLinkItemSelectionModel is "pure C++ and Qt"

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              F 1 Reply Last reply
              0
              • VRoninV VRonin

                @firen said in Sync selectionModel between two views where one view has a transposed Model of the other:

                I dont want to import KDE code

                You really prefer code posted by randos on a forum to just using classes tried and tested by a major organisation?

                only pure C++ and Qt

                KLinkItemSelectionModel is "pure C++ and Qt"

                F Offline
                F Offline
                firen
                wrote on last edited by
                #7

                @VRonin it is for my work (and i am a beginner) and not sure if i am aloud to use that (cause of copyright etc.)

                But in general i found another solution, because I "switch" between the views so it is always only one view visible.

                So i just:

                1. reset the selectionmodel of the view which is going to be visible
                2. read the selectedIndexes of the view which is going to be hidden
                3. iterate over the QModelIndexList i got and set the selectionModel of the visible view

                That seems ok for my needs

                VRoninV 1 Reply Last reply
                0
                • F firen

                  Hello @JonB,

                  yes I dont want to import KDE code (only pure C++ and Qt).
                  Could you give me a hint what exactly I have to rewrite?

                  I tested for example:

                  connect(original_View->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
                    transposed_View, SLOT(onSelectionChanged(const QModelIndex &, const QModelIndex &)));
                  

                  And in on SelectionChanged I use something like:

                  void CTableWidgetView::onSelectionChanged(const QModelIndex & originalIndexSelected, const QModelIndex & originalIndexDeselected)
                  {
                  
                    selectionModel()->setCurrentIndex(model()->index(originalIndexSelected.column(), 
                      originalIndexSelected.row()), QItemSelectionModel::Select);
                  
                    
                    selectionModel()->setCurrentIndex(model()->index(originalIndexDeselected.column(),
                      originalIndexDeselected.row()), QItemSelectionModel::Deselect);
                  
                  }
                  

                  But this works only in one direction and only if i select only ONE cell.

                  Thank you!

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @firen
                  FWIW, if @VRonin recommends using the written KDE solution I'd be looking at using that. You might be able to just take the necessary code for item selection and throw away other stuff if it comes with other large code you don't need.

                  1 Reply Last reply
                  0
                  • F firen

                    @VRonin it is for my work (and i am a beginner) and not sure if i am aloud to use that (cause of copyright etc.)

                    But in general i found another solution, because I "switch" between the views so it is always only one view visible.

                    So i just:

                    1. reset the selectionmodel of the view which is going to be visible
                    2. read the selectedIndexes of the view which is going to be hidden
                    3. iterate over the QModelIndexList i got and set the selectionModel of the visible view

                    That seems ok for my needs

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @firen said in Sync selectionModel between two views where one view has a transposed Model of the other:

                    That seems ok for my needs

                    Try inserting/removing rows/columns or sorting and you'll see it will break

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved