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. QTreeWidget: how to prevent selection change by user
QtWS25 Last Chance

QTreeWidget: how to prevent selection change by user

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 4.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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by Diracsbracket
    #1

    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!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chrisw01
      wrote on last edited by Chrisw01
      #2

      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.

      DiracsbracketD 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        DiracsbracketD 1 Reply Last reply
        0
        • DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • C Chrisw01

            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.

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #5

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

            setSelectionMode

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

            VRoninV 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

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

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by
              #6

              @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).

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • DiracsbracketD Diracsbracket

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

                  setSelectionMode

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

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

                  @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"?

                  "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

                  DiracsbracketD 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @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"?

                    DiracsbracketD Offline
                    DiracsbracketD Offline
                    Diracsbracket
                    wrote on last edited by Diracsbracket
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • DiracsbracketD Offline
                      DiracsbracketD Offline
                      Diracsbracket
                      wrote on last edited by Diracsbracket
                      #10

                      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!

                      1 Reply Last reply
                      2

                      • Login

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