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. How to keep selection on one Qtableview while operating on a another
Forum Update on Monday, May 27th 2025

How to keep selection on one Qtableview while operating on a another

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.6k 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.
  • AsimovA Offline
    AsimovA Offline
    Asimov
    wrote on last edited by
    #1

    Hi all,
    I am new to this forum so I hope I am posting in the right place.
    I have a qt application with two qtableviews made from a model.

    When I select a row on one table it shows me a list of that item in the second table.
    However when I start editing the second table I lose the selected row in the first table.

    Is there anyway to keep the selection activated on the first table while editing the contents on the second table?
    0_1491302855010_Screenshot 2017-04-04 11.38.28.png

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

      could you post the code you are using?

      Is the model a tree and you are showing top level in one table and child items in another?

      "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
      0
      • AsimovA Offline
        AsimovA Offline
        Asimov
        wrote on last edited by
        #3

        There is two tables being shown. One table is referentially linked to the other table in sqlite.
        I have run some code to check if the first table item is still selected, but when I click on the other table the blue select colour disappears, but I know the item you see in the picture is still selected, as my code is telling me the item is still selected.

        Basically the table on the left shows my boxes, and the table on the right is supposed to show the contents of the boxes, and that bit is working fine.

        What I want is that the background colour of the selected item remains visually selected while I edit the contents on the right hand table.

        I believe now maybe I need to change the background colour of the row when I click on it, rather than just rely on the built in highlighter. Although I don't know how to do that either.

        So basically I want to visually keep the line blue when I click on the other table to the right, but the blue is just the standard row selection. So I think I may have to manually add a colour to the background of the row. I hope I am clear, as I am not good at explaining myself mostly.

        I am not sure which code I can show that will help the answer. I can show you my left hand box loading function if that will shed any light on anything.

        void DbManager::boxList(Ui::MainWindow* ui){
            modelBox=new QSqlTableModel();
            modelBox->setTable("boxes");
            modelBox->select();
            modelBox->setHeaderData(1, Qt::Horizontal, QObject::tr("Box ID / Name"));
            modelBox->setHeaderData(2, Qt::Horizontal, QObject::tr("Box Location"));
            ui->boxTable->setModel(modelBox);
            ui->boxTable->setColumnHidden(0, true);
        }
        
        1 Reply Last reply
        0
        • AsimovA Offline
          AsimovA Offline
          Asimov
          wrote on last edited by
          #4

          I will answer my own question. When you click on the other table the selection on the first table is really still selected, but you can no longer see it. The answer is to change the highlight palette and this solved my problem. Now the highlighted row looks like it is still selected, which is what I wanted.

          QPalette* palette = new QPalette();
              palette->setColor(QPalette::Highlight,QColor("#3a7fc2"));
              palette->setColor(QPalette::HighlightedText, QColor("white"));
              ui->boxTable->setPalette(*palette);
              ui->contentTable->setPalette(*palette);
          
          
          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