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



  • 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



  • 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?



  • 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);
    }
    


  • 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);
    
    

Log in to reply