QTreeWidget: how to prevent selection change by user



  • Hi,
    I am using a QTreeWidget as a multi-column list. The row selection within the widget is controlled by a script, which sequentially processes a row at the time, selecting it in the process to provide feedback to the user as to which row is being processed.

    I want to prevent the user of making any selection: the selection may only be done by the program, but the user may still scroll the list to see other rows.

    How can I achieve this?

    Thanks!



  • Hi,

    Take a look at selectionMode and or setSelectionMode. I'm not sure if this would disable it for just the user or both user and system.


  • Lifetime Qt Champion

    Hi,

    Then why not disable the widget while the processing is being done ?



  • This post is deleted!


  • @Chrisw01 said in QTreeWidget: how to prevent selection change by user:

    setSelectionMode

    Setting setSelectionMode to NoSelection also disables selection higlighting by the code.



  • @SGaist If I disable the widget, the scroll does not work, and I need to override the stylesheet to give the disabled widget the same look as the enabled one (which is not a big deal, I suppose).


  • Lifetime Qt Champion

    Then you can install an event filter that will eat the keys and mouse related events.



  • @Diracsbracket said in QTreeWidget: how to prevent selection change by user:

    Setting setSelectionMode to NoSelection also disables selection higlighting by the code

    How do you implement the "higlighting by the code"?



  • @VRonin said in QTreeWidget: how to prevent selection change by user:

    How do you implement the "higlighting by the code"?

    Just by selecting the row using setCurrentItem.

    ui->treeWidget->setCurrentItem(ui->treeWidget->topLevelItem(scriptLineIndex));
    

    The default styleSheet for the selected state (which you can override) applies then, which is different from the not-selected state.



  • I finally chose the following option:
    I made all items in the QTreeWidget enabled, but non-selectable:

    itm->setFlags(Qt::ItemIsEnabled);
    

    This still allows for row highlighting upon mouse hovering, which is good, and I can still scroll the list.

    Then, using two variables to hold the indices of the currently selected row, and the previously selected row, I apply my formatting directly:

    • the currently selected row is highlighted
    • the color formatting of the previously selected row is restored
        setRowColor(prevLineIndex, Qt::white, Qt::black);
        setRowColor(scriptLineIndex, Qt::blue, Qt::white);
    

    setRowColor does the following:

    void MainWindow::setRowColor(int rowIndex, const QColor& backColor, const QColor& textColor)
    {
        if ((rowIndex<0) || (rowIndex>=numLines))
            return;
    
        for (int i=0; i<UM_NUM_TW_COL;i++)
        {
            ui->treeWidget->topLevelItem(rowIndex)->setBackgroundColor(i, backColor);
            ui->treeWidget->topLevelItem(rowIndex)->setTextColor(i, textColor);
        }
    }
    

    Thanks for your feedback everyone!


Log in to reply
 

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