Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deselect *visually* at start of program
Qt 6.12 LTS is out! See what's new in the release blog

Deselect *visually* at start of program

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

    I have a couple of QTreeWidgets in my UI, which I fill when I init my MainWindow (Creation Process: Construct -> Init -> Show) then I always get a ghost selection in all TWs.

    When I fill the data into the TWs, I surround it with:

    ui->itemList->clear(); 
    ui->itemList->clearSelection();
    ui->itemList->blockSignals(true);
    // Fill data
    ui->itemList->blockSignals(false);
    

    (itemList = one of my TWs)

    While clearSelection does clear the selection for the code (ui->itemTree->selectedItems().isEmpty()) it does not do that visually7928c500-8322-416a-bc08-b46e621ad0fe-image.png

    The item looks selected, but isn't according to the code

    bool noItemSel = ui->itemList->selectedItems().isEmpty();
    

    This variable gets used to enable/disable certain buttons which should only work if an item is selected:

        ui->pbItemAdd->setDisabled(noTreeRights);
        ui->pbItemEdit->setDisabled(noItemSel || noTreeRights);
        ui->pbItemDelete->setDisabled(noItemSel || noTreeRights);
    

    these buttons, shown here
    6d1be532-9af5-47bd-9d3a-4b5a20ada45a-image.png
    get disabled, noTreeRights is false, as seen by the add button, so that one doesnt cause the issue of disabling. Theres a lot of other loading done when someone selects an Item, so I'd rather keep the tree widget unselected than selecting the first item

    So how do I remove that visual selection?

    Axel SpoerlA 1 Reply Last reply
    0
    • G GKLC

      I have a couple of QTreeWidgets in my UI, which I fill when I init my MainWindow (Creation Process: Construct -> Init -> Show) then I always get a ghost selection in all TWs.

      When I fill the data into the TWs, I surround it with:

      ui->itemList->clear(); 
      ui->itemList->clearSelection();
      ui->itemList->blockSignals(true);
      // Fill data
      ui->itemList->blockSignals(false);
      

      (itemList = one of my TWs)

      While clearSelection does clear the selection for the code (ui->itemTree->selectedItems().isEmpty()) it does not do that visually7928c500-8322-416a-bc08-b46e621ad0fe-image.png

      The item looks selected, but isn't according to the code

      bool noItemSel = ui->itemList->selectedItems().isEmpty();
      

      This variable gets used to enable/disable certain buttons which should only work if an item is selected:

          ui->pbItemAdd->setDisabled(noTreeRights);
          ui->pbItemEdit->setDisabled(noItemSel || noTreeRights);
          ui->pbItemDelete->setDisabled(noItemSel || noTreeRights);
      

      these buttons, shown here
      6d1be532-9af5-47bd-9d3a-4b5a20ada45a-image.png
      get disabled, noTreeRights is false, as seen by the add button, so that one doesnt cause the issue of disabling. Theres a lot of other loading done when someone selects an Item, so I'd rather keep the tree widget unselected than selecting the first item

      So how do I remove that visual selection?

      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      @GKLC
      Thanks. Am I overlooking something? I don’t find a question in your post!

      Software Engineer
      The Qt Company, Oslo

      G 1 Reply Last reply
      0
      • Axel SpoerlA Axel Spoerl

        @GKLC
        Thanks. Am I overlooking something? I don’t find a question in your post!

        G Offline
        G Offline
        GKLC
        wrote on last edited by
        #3

        @Axel-Spoerl I added the obvious question now.

        J.HilkJ 1 Reply Last reply
        0
        • G GKLC

          @Axel-Spoerl I added the obvious question now.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @GKLC that item is probably the "currentItem"
          and since your selection mode is not NoSelection the item is also "selected" aka highlighted


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          G 1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @GKLC that item is probably the "currentItem"
            and since your selection mode is not NoSelection the item is also "selected" aka highlighted

            G Offline
            G Offline
            GKLC
            wrote on last edited by
            #5

            @J-Hilk I did ui->itemList->setCurrentItem(nullptr) now, and it still is highlighted

            JonBJ 1 Reply Last reply
            0
            • G GKLC

              @J-Hilk I did ui->itemList->setCurrentItem(nullptr) now, and it still is highlighted

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @GKLC
              While you wait for an answer, what about a bit more detective work?

              • Move the clearSelection() to after the blockSignals() and data filling?
              • Can you temporarily comment out the blockSignals(), does that change behaviour?
              • Try selecting a different item in code. (Note: I think that requires changing the selection, not the currrent item.) Does the blue item move?
              • Is there any chance the first item in the list gets auto-/re-selected when first shown, independent of your code setting (I don't know)?
              • Try moving any selecting or deselecting code into a slot executed on a QTimer::singleShot() with a delay of 0 or small to execute after your filling code. Does that work?
              G Axel SpoerlA 2 Replies Last reply
              0
              • JonBJ JonB

                @GKLC
                While you wait for an answer, what about a bit more detective work?

                • Move the clearSelection() to after the blockSignals() and data filling?
                • Can you temporarily comment out the blockSignals(), does that change behaviour?
                • Try selecting a different item in code. (Note: I think that requires changing the selection, not the currrent item.) Does the blue item move?
                • Is there any chance the first item in the list gets auto-/re-selected when first shown, independent of your code setting (I don't know)?
                • Try moving any selecting or deselecting code into a slot executed on a QTimer::singleShot() with a delay of 0 or small to execute after your filling code. Does that work?
                G Offline
                G Offline
                GKLC
                wrote on last edited by
                #7

                @JonB First 3 things do nothing, whether all together or singularly, the first item still is highlighted but not selected, which seems to confirm the 4th item that you suggested. The first item gets highlighted, though not selected (debugger confirms)

                1 Reply Last reply
                0
                • JonBJ JonB

                  @GKLC
                  While you wait for an answer, what about a bit more detective work?

                  • Move the clearSelection() to after the blockSignals() and data filling?
                  • Can you temporarily comment out the blockSignals(), does that change behaviour?
                  • Try selecting a different item in code. (Note: I think that requires changing the selection, not the currrent item.) Does the blue item move?
                  • Is there any chance the first item in the list gets auto-/re-selected when first shown, independent of your code setting (I don't know)?
                  • Try moving any selecting or deselecting code into a slot executed on a QTimer::singleShot() with a delay of 0 or small to execute after your filling code. Does that work?
                  Axel SpoerlA Offline
                  Axel SpoerlA Offline
                  Axel Spoerl
                  Moderators
                  wrote on last edited by
                  #8

                  First of all, as @J-Hilk pointed out, disable selections by
                  myTreeview->setSelectionMode(QAbstractItemView::NoSelection);

                  Nothing can be selected from now on.
                  What may look a bit like a selection is the currentItem.
                  That should not be highlighted, unless the tree widget has focus.
                  It is usually rendered with a visible frame, indicating that the frame can be moved with the up/down keys and the item can be expanded/collapsed with the left/right keys.

                  Software Engineer
                  The Qt Company, Oslo

                  G 1 Reply Last reply
                  3
                  • Axel SpoerlA Axel Spoerl

                    First of all, as @J-Hilk pointed out, disable selections by
                    myTreeview->setSelectionMode(QAbstractItemView::NoSelection);

                    Nothing can be selected from now on.
                    What may look a bit like a selection is the currentItem.
                    That should not be highlighted, unless the tree widget has focus.
                    It is usually rendered with a visible frame, indicating that the frame can be moved with the up/down keys and the item can be expanded/collapsed with the left/right keys.

                    G Offline
                    G Offline
                    GKLC
                    wrote on last edited by
                    #9

                    @Axel-Spoerl I've tried setting the current item to a nullptr, which didn't help

                    I do need to be able to set a selection (SingleSelection specifically), at the start of the program however, I don't want anything selected, as it makes it confusing for users.

                    Its also definitely a selection if the current item only has that frame, that is, if the selected item is the highlighted blue item shown in the picture

                    JonBJ 1 Reply Last reply
                    0
                    • G GKLC

                      @Axel-Spoerl I've tried setting the current item to a nullptr, which didn't help

                      I do need to be able to set a selection (SingleSelection specifically), at the start of the program however, I don't want anything selected, as it makes it confusing for users.

                      Its also definitely a selection if the current item only has that frame, that is, if the selected item is the highlighted blue item shown in the picture

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @GKLC
                      By now I suggest you produce a 20-odd line standalone minimal example. Reduce to absolute least which produces behaviour. May not apply to you, but 50% of the time we ask for this the OP discovers the issue. Try for yourself just starting anew from a QTreeWidget.

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