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. Change icons in columns on QTreeWidget

Change icons in columns on QTreeWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 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
    gabello306
    wrote on last edited by
    #1

    I have a QTreeWidget and ite has four columns. I have established icons for each item in the each column. I am trying to change the icon to an open folder when I expand a QTreeWidgetItem. I need to know what column I'm in to change the icon. I'm using the following for my connect and calling my method associated with the signal.

     connect(ui->launchTree, &QTreeWidget::itemExpanded, this, &NotebookView::OnItemExpanded);
    
    

    The method:

    void NotebookView::OnItemExpanded(QTreeWidgetItem *item)
    {
        int columnCount = ui->launchTree->columnCount();
    
    }
    

    The method is being called but everything I have tried does not allow me to change the con except on the top level item item.

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

      Hi,

      What exactly did you do ?
      The code you show just retrieves the column count.

      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
      0
      • G gabello306

        I have a QTreeWidget and ite has four columns. I have established icons for each item in the each column. I am trying to change the icon to an open folder when I expand a QTreeWidgetItem. I need to know what column I'm in to change the icon. I'm using the following for my connect and calling my method associated with the signal.

         connect(ui->launchTree, &QTreeWidget::itemExpanded, this, &NotebookView::OnItemExpanded);
        
        

        The method:

        void NotebookView::OnItemExpanded(QTreeWidgetItem *item)
        {
            int columnCount = ui->launchTree->columnCount();
        
        }
        

        The method is being called but everything I have tried does not allow me to change the con except on the top level item item.

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

        @gabello306
        Before we figure some way to get the column number from the QTreeWidgetItem[*], you can see from the list of signals at https://doc.qt.io/qt-6/qtreewidget.html#signals that all the other ones which expect your slot to care about the column pass it as a parameter. They clearly do not think it should be relevant during itemCollapsed/Expanded(), so why do you need it there?

        [*] QModelIndex QTreeWidget::indexFromItem(const QTreeWidgetItem *item, int column = 0) const

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gabello306
          wrote on last edited by
          #4

          I am converting an old application using Adobe Air. The tree uses folder, folder-open and file icons in the tree. When a part of the tree is expanded the closed folder changes to and open folder icon. I need to do the same. I have used wxWidgets to convert the program but now I'm looking at Qt because it seems to have more of the options I need. In essence when I expand an item, if it has another folder under it, the icon will change to open folder and when it gets to the bottom of the tree it will display a file icon. To accomplish this, I need to know the column number that was expanded.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gabello306
            wrote on last edited by
            #5

            I have found that using the double-click works fine but when I use the expand arrows in the tree the signal does not work. I have tried several of the signals JonB has recommended and none of them seem to work using the arrows to expand the tree.

            JonBJ 1 Reply Last reply
            0
            • G gabello306

              I have found that using the double-click works fine but when I use the expand arrows in the tree the signal does not work. I have tried several of the signals JonB has recommended and none of them seem to work using the arrows to expand the tree.

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

              @gabello306
              Does the solution at https://stackoverflow.com/a/41180101/489865 do what you want?

              That may work (the second case) without needing signals? Or maybe not? I don't know what signals you are not getting, and to be honest I don't know what "using the arrows to expand the tree" means exactly.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gabello306
                wrote on last edited by
                #7

                In your suggestion example, if you look at the top there are open triangles (mine are filled). When I use those I don't get a signal unless I use the itemExpanded which doesn't provide a column. I can set the icon when I load the data, that isn't the problem. When I have loaded data and start opening the tree then I need the column to change the icon as in his second picture in the example.

                B 1 Reply Last reply
                0
                • G gabello306

                  In your suggestion example, if you look at the top there are open triangles (mine are filled). When I use those I don't get a signal unless I use the itemExpanded which doesn't provide a column. I can set the icon when I load the data, that isn't the problem. When I have loaded data and start opening the tree then I need the column to change the icon as in his second picture in the example.

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  @gabello306 Why do you need column for itemExpanded?
                  It is not provided because Expanded/Collapsed is for the whole row, not one single column.
                  I really cannot understand your need from your text. Maybe you could post more information like screenshots or codes.

                  JonBJ 1 Reply Last reply
                  1
                  • B Bonnie

                    @gabello306 Why do you need column for itemExpanded?
                    It is not provided because Expanded/Collapsed is for the whole row, not one single column.
                    I really cannot understand your need from your text. Maybe you could post more information like screenshots or codes.

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

                    @Bonnie said in Change icons in columns on QTreeWidget:

                    @gabello306 Why do you need column for itemExpanded?
                    It is not provided because Expanded/Collapsed is for the whole row, not one single column.

                    That is exactly what I asked earlier. Those signals which need/work on a column have it as a parameter. Expand/collapse is by row, not by column, hence does not. I too do not understand what OP wants column for: when you click on an "arrow" it does not matter, and should not matter, what column that is in, just the row it is on, which you get for the QTreeWidgetItem.

                    Does QFileSystemModel (with a QTreeView) do whatever it is you want about opening/closing folder icons?

                    I (perhaps like @Bonnie) am unclear how you are handling these "expansion" items. You are supposed to do this via QTreeWidgetItem::setIcon(), setting the inbuilt icon for the whole QTreeWidgetItem. You don't care what column/indent level/depth this appears at, Qt handles that for you. But the way you mention "four columns", and want to get a column index, might imply you are handling the expansion icons as their own column yourself? We do not have information to know. If you are doing the first, intended way then it seems to me the code at https://stackoverflow.com/a/46834929/489865 shows you how to handle the itemExpanded/Collapsed signals to change the QTreeWidgetItem's icon without any reference to column number.

                    I would recommend the foregoing. If you really, really want to handle icons on your own columns then you need to know the depth of the QTreeWidgetItem received as parameter to itemExpanded/Collapsed slot. Since I do not see any method on QTreeWidgetItem or QTreeWidget to return this, you can loop upwards off QTreeWidgetItem::parent() until that returns nullptr (top-level item). The number of iterations is the depth of the item, and that is the column where your indicator icon lives.

                    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