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. How to delete all QTreeWidgetItems having the same text as selected QTreeWidgetItem?
Forum Updated to NodeBB v4.3 + New Features

How to delete all QTreeWidgetItems having the same text as selected QTreeWidgetItem?

Scheduled Pinned Locked Moved General and Desktop
qtreewidgetqtreewidgetitem
24 Posts 2 Posters 11.2k Views 2 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.
  • SGaistS SGaist

    You don't, that signal just tells you that something has changed selection wise. Reaction to a click on an empty space is handled by the widget.

    If you have the child deeper than the second level, then yes you would need to go back up to the top level item.

    You're welcome !

    WDR_937W Offline
    WDR_937W Offline
    WDR_937
    wrote on last edited by
    #14

    @SGaist Is this the right way of doing it?

    connect(ui->Usable_Resource_Tree, SIGNAL(ui->Usable_Resource_Tree->selectionModel()->selectionChanged()), this, SLOT(Disable_Button()));
    
    void Disable_Button()
    {
        ui->Delete_Button->setEnabled(false);
    }
    

    I haven't tested this. But I'm pretty sure something is definitely not right here. Please tell me whether my code is correct. Thanks.

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

      No it's not,

      connect(ui->Usable_Resource_Tree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateDeleteButton(QItemSelection, QItemSelection)));
      
      void updateDeleteButton(const QItemSelection &selected, const QItemSelection &deselected)
      {
          ui->Delete_Button->setEnabled(selected.size() > 0);
      }
      

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

      WDR_937W 1 Reply Last reply
      0
      • SGaistS SGaist

        No it's not,

        connect(ui->Usable_Resource_Tree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateDeleteButton(QItemSelection, QItemSelection)));
        
        void updateDeleteButton(const QItemSelection &selected, const QItemSelection &deselected)
        {
            ui->Delete_Button->setEnabled(selected.size() > 0);
        }
        
        WDR_937W Offline
        WDR_937W Offline
        WDR_937
        wrote on last edited by
        #16

        @SGaist OK... So, I implemented what you posted here. It's working with selecting the items and is also being deselected when the item is removed. However, the clicking on empty space thing is still not implemented. How should this be implemented? Should I manually check for it or is it done automatically by the signal so that all I should do is just call it?

        The deselected variable is not being used, so I set it to Q_UNUSED. Is it needed anywhere? Also, since I am calling this signal/slot, do I need to check for Item->isSelected() later on in my delete button slot? Why I ask this, is because the program crashed earlier before I even implemented Item->isSelected(). That is why I implemented it.

        Finally, I am encountering two anomalous behaviors:-

        1. The selectionChanged() is deselecting on removing the child items, but for parent items, it is by default selecting the next parent in the widget. How to disable this?

        2. Sometimes, instead of graying out and flattening out on disabling, the delete button does not flatten out and becomes blue like it is when highlighted by the mouse. The text, however, grays out. It stays like this until it is enabled. This is only cosmetic as the button is still disabled. But it looks weird. Any way to fix this?

        My apologies if I asked too many questions. I just want to solve this tree problem. Please bear with me. Thank you. :-)

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

          IIRC, you need to reimplement keyPressEvent and call clearSelection in it for that.

          Q_UNUSED is the right tool, deselected is not needed in that case.

          Since I don't know how you are currently performing the delete I can't comment on that one.

          1. Before performing the delete, gather all informations, clear the selection, perform the deletion
          2. Haven't seen that one happen

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

          WDR_937W 1 Reply Last reply
          0
          • SGaistS SGaist

            IIRC, you need to reimplement keyPressEvent and call clearSelection in it for that.

            Q_UNUSED is the right tool, deselected is not needed in that case.

            Since I don't know how you are currently performing the delete I can't comment on that one.

            1. Before performing the delete, gather all informations, clear the selection, perform the deletion
            2. Haven't seen that one happen
            WDR_937W Offline
            WDR_937W Offline
            WDR_937
            wrote on last edited by
            #18

            @SGaist said:

            IIRC, you need to reimplement keyPressEvent and call clearSelection in it for that.

            Do you mean QMouseEvent? Because I am clicking on empty space, not pressing any keys. Unless, the keyPressEvent is also used in this case, for which I don't know. Also, how would I know whether I am clicking on empty space or an item? Same selection.size > 0? Please clarify.

            @SGaist said:

            Since I don't know how you are currently performing the delete I can't comment on that one.

            Here's what I'm doing,

            QTreeWidgetItem *Resource = From_Tree->currentItem();
            
            if(Resource != NULL)
            	{
            		if(Resource->isSelected())
            		{
            			From_Tree->takeTopLevelItem(From_Tree->indexOfTopLevelItem(Resource));
            		}
            	}
            

            I remember why I implemented the isSelected(). When clicked, the button was removing the first item by default if nothing was selected. Since I am disabling the button completely when nothing is selected, I don't think I'll need this. Correct me if I'm wrong. Thanks.

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

              mousePressEvent indeed… Use itemAt to see if you have something under the mouse

              You should rather user selectedItems

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

              WDR_937W 1 Reply Last reply
              0
              • SGaistS SGaist

                mousePressEvent indeed… Use itemAt to see if you have something under the mouse

                You should rather user selectedItems

                WDR_937W Offline
                WDR_937W Offline
                WDR_937
                wrote on last edited by WDR_937
                #20

                @SGaist said:

                mousePressEvent indeed… Use itemAt to see if you have something under the mouse

                Alright, thanks. I'll look into that. I'll see how it goes. If I hit any roadblock again, I'll ask here.

                @SGaist said:

                You should rather user selectedItems

                Are you talking about the QTreeWidgetItem *Resource = From_Tree->currentItem() part of my code?

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

                  No, selectedItems

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

                  WDR_937W 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    No, selectedItems

                    WDR_937W Offline
                    WDR_937W Offline
                    WDR_937
                    wrote on last edited by
                    #22

                    @SGaist Yeah, I understood what you said. I was asking do you mean that part of my code?

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

                      Ok, then yes, to replace currentItem

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

                      WDR_937W 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Ok, then yes, to replace currentItem

                        WDR_937W Offline
                        WDR_937W Offline
                        WDR_937
                        wrote on last edited by
                        #24

                        @SGaist Oh, OK... Then I'll implement that too and see how it goes. Thanks.

                        1 Reply Last reply
                        0

                        • Login

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