Change icons in columns on QTreeWidget
-
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.
-
Hi,
What exactly did you do ?
The code you show just retrieves the column count. -
@gabello306
Before we figure some way to get the column number from theQTreeWidgetItem
[*], 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 duringitemCollapsed
/Expanded()
, so why do you need it there?[*] QModelIndex QTreeWidget::indexFromItem(const QTreeWidgetItem *item, int column = 0) const
-
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.
-
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.
-
@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.
-
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.
-
@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. -
@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 aQTreeView
) 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 wholeQTreeWidgetItem
. 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 theitemExpanded
/Collapsed
signals to change theQTreeWidgetItem
'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 toitemExpanded
/Collapsed
slot. Since I do not see any method onQTreeWidgetItem
orQTreeWidget
to return this, you can loop upwards offQTreeWidgetItem::parent()
until that returnsnullptr
(top-level item). The number of iterations is the depth of the item, and that is the column where your indicator icon lives.