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
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget: how to prevent selection change by user

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 4.8k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Diracsbracket
    wrote on 9 Sept 2017, 13:59 last edited by Diracsbracket 9 Sept 2017, 13:59
    #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 9 Sept 2017, 14:27 last edited by Chrisw01 9 Sept 2017, 14:28
      #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.

      D 1 Reply Last reply 10 Sept 2017, 02:38
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 9 Sept 2017, 20:28 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

        D 1 Reply Last reply 10 Sept 2017, 02:38
        0
        • D Offline
          D Offline
          Diracsbracket
          wrote on 10 Sept 2017, 02:35 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • C Chrisw01
            9 Sept 2017, 14:27

            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.

            D Offline
            D Offline
            Diracsbracket
            wrote on 10 Sept 2017, 02:38 last edited by Diracsbracket 9 Oct 2017, 02:39
            #5

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

            setSelectionMode

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

            V 1 Reply Last reply 11 Sept 2017, 10:24
            0
            • S SGaist
              9 Sept 2017, 20:28

              Hi,

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

              D Offline
              D Offline
              Diracsbracket
              wrote on 10 Sept 2017, 02:38 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 10 Sept 2017, 20:50 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
                • D Diracsbracket
                  10 Sept 2017, 02:38

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

                  setSelectionMode

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

                  V Offline
                  V Offline
                  VRonin
                  wrote on 11 Sept 2017, 10:24 last edited by VRonin 9 Nov 2017, 10:25
                  #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

                  D 1 Reply Last reply 13 Sept 2017, 04:34
                  0
                  • V VRonin
                    11 Sept 2017, 10:24

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

                    D Offline
                    D Offline
                    Diracsbracket
                    wrote on 13 Sept 2017, 04:34 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
                    • D Offline
                      D Offline
                      Diracsbracket
                      wrote on 13 Sept 2017, 04:47 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

                      9/10

                      13 Sept 2017, 04:34

                      • Login

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