QTreeView and QListWidget, how to keep selection after lost focus?



  • Hi,

    I have a QTreeView and a QListWidget. Now after I select some rows in them, after they loses focus, the selection becomes invalid.
    How to keep selection after they lose focus?
    I didn't find anything from Qt document.

    Thanks



  • @wqking
    For QTreeView you can try this .
    ui->QTreeView->selectionModel()->select(child->index(),QItemSelectionModel::ClearAndSelect);



  • @Ratzz said:

    @wqking
    For QTreeView you can try this .
    ui->QTreeView->selectionModel()->select(child->index(),QItemSelectionModel::ClearAndSelect);

    Thanks, but I don't think it's the solution.
    The selection is selected by the user, not by the code.
    What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white. What I want to do it the item keeps selected after the user clicks other place.



  • This post is deleted!


  • @wqking said:

    What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white.

    So you want the background to be blue even if user clicks outside??
    What i feel is that it is not background change it is losing focus.



  • @Ratzz said:

    @wqking said:

    What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white.

    So you want the background to be blue even if user clicks outside??

    Exactly. The background remains blue after lost focus.

    I searched and found that we can re-set the selection (like your first reply) in FocusOut event, but it didn't work after I tried.



  • @wqking said:

    but in current version, after the user clicks any elsewhere,

    Anywhere in the sense other widget in your main window or outside the treeview?



  • @Ratzz said:

    @wqking said:

    but in current version, after the user clicks any elsewhere,

    Anywhere in the sense other widget in your main window or outside the treeview?

    I want both.



  • @wqking
    You can cheat a bit with QTimer.



  • @wqking

    Hi,

    are You sure that the selection is lost when the view loses focus?
    In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
    If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

    hope this helps
    hi
    riki



  • @Riki-P said:

    @wqking

    Hi,

    are You sure that the selection is lost when the view loses focus?
    In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
    If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

    hope this helps
    hi
    riki

    The selection is not lost after lost focus, but the color changes, same as what you said.

    I will investigate in style sheet. But can you share how you did with style sheet? I have no any experience on Qt style sheet.

    Thanks


  • Qt Champions 2016

    Out of the top of my head, you could use an QItemDelegate and provide your own background drawing for the given model index. I think it should work for your case.

    Kind regards.



  • @Riki-P said:

    @wqking

    Hi,

    are You sure that the selection is lost when the view loses focus?
    In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
    If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

    hope this helps
    hi
    riki

    I think QListWidget::item:selected:!active in style sheet should do the work. But how to make it using the active color instead of a hard coded color?



  • @wqking

    Hi, I have a minimal qss file where I added the following lines:

    QTableView {
        selection-background-color: %TableViewInactiveHighLightedBackground%;
        selection-color: %TableViewInactiveHighLightedText%;
    }
    QTableView:active {
        selection-background-color: %TableViewHighLightedBackground%;
        selection-color: %TableViewHighLightedText%;
    }
    

    and on program startup i change the placeholders %....% with the actual values:

    fileName = ":/qss/QssFileName.qss";
    QFile qssFile(fileName);
    QString qss;
    
    if (qssFile.open(QFile::ReadOnly))
    {
        QTextStream qssStream(&qssFile);
        qss = qssStream.readAll();
    
        // customize the QSS file contents to use the correct values for the highlighted rows in table views
        QPalette p = QApplication::palette("MainWindow");
        qss.replace("%TableViewInactiveHighLightedBackground%", toQssFormat(p.color(QPalette::Active, QPalette::Highlight).lighter()));
        qss.replace("%TableViewInactiveHighLightedText%",       toQssFormat(p.color(QPalette::Active, QPalette::HighlightedText).darker()));
        qss.replace("%TableViewHighLightedBackground%",         toQssFormat(p.color(QPalette::Active, QPalette::Highlight)));
        qss.replace("%TableViewHighLightedText%",               toQssFormat(p.color(QPalette::Active, QPalette::HighlightedText)));
    }
    

    toQssFormat is just

    QString toQssFormat(const QColor &color)
    {
        return QString("rgba(%1, %2, %3, %4)")
                .arg(color.red())
                .arg(color.green())
                .arg(color.blue())
                .arg(color.alpha());
    }
    

    Hope this helps
    bye
    riki


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.