How to control collapse/expand in QTreeView
-
Hi All,
Is it possible to disable expand/collapse control mechanism for my current directory in QTreeView, even if my current directory has sub-directories and files ???My QTreeView has QFileSystemModel as its model.
Thank You!
-
Hi All,
Is it possible to disable expand/collapse control mechanism for my current directory in QTreeView, even if my current directory has sub-directories and files ???My QTreeView has QFileSystemModel as its model.
Thank You!
@Vinoth-Rajendran4
Well, I don't know if there is a neater way to do it, but I assume you can always define your own slot for http://doc.qt.io/qt-5/qtreeview.html#expand (and others) and have it not call the base slot on certain nodes of your choice? -
@Vinoth-Rajendran4
Well, I don't know if there is a neater way to do it, but I assume you can always define your own slot for http://doc.qt.io/qt-5/qtreeview.html#expand (and others) and have it not call the base slot on certain nodes of your choice?@JonB : Thanks for your reply. Can you please provide an abstract code, of blocking base slot .
An simple example to get me started. -
@JonB : Thanks for your reply. Can you please provide an abstract code, of blocking base slot .
An simple example to get me started.@Vinoth-Rajendran4
I don't do C++. You'll have to fill in details/corrections.-
Don't use a
QTreeView
directly if you are doing that at present. Create a sub-classMyTreeView
deriving fromQTreeView
. -
In that sub-class, override the
expand
slot (http://doc.qt.io/qt-5/qtreeview.html#expand). https://stackoverflow.com/questions/29170751/override-qt-slot-in-subclass may help you. You probably only need to do like https://stackoverflow.com/a/29172350/489865. -
Normally your override method would look like
void MyTreeView::expand(const QModelIndex &index) { QTreeView::expand(index); }
i.e. it's your job to call the base method to actually still do the expand if you override.
Try commenting out the call to the base
QTreeView::expand(index);
. This should completely stop the tree view from actually expanding nodes, right? Then do whatever to check whether theindex
parameter refers to your current directory, and make it so in that case only it does not do the expand. So it ends up likevoid MyTreeView::expand(const QModelIndex &index) { if (isCurrentDirectoryIndex(index)) return; QTreeView::expand(index); }
Then you'll be done :)
-
-
@Vinoth-Rajendran4
I don't do C++. You'll have to fill in details/corrections.-
Don't use a
QTreeView
directly if you are doing that at present. Create a sub-classMyTreeView
deriving fromQTreeView
. -
In that sub-class, override the
expand
slot (http://doc.qt.io/qt-5/qtreeview.html#expand). https://stackoverflow.com/questions/29170751/override-qt-slot-in-subclass may help you. You probably only need to do like https://stackoverflow.com/a/29172350/489865. -
Normally your override method would look like
void MyTreeView::expand(const QModelIndex &index) { QTreeView::expand(index); }
i.e. it's your job to call the base method to actually still do the expand if you override.
Try commenting out the call to the base
QTreeView::expand(index);
. This should completely stop the tree view from actually expanding nodes, right? Then do whatever to check whether theindex
parameter refers to your current directory, and make it so in that case only it does not do the expand. So it ends up likevoid MyTreeView::expand(const QModelIndex &index) { if (isCurrentDirectoryIndex(index)) return; QTreeView::expand(index); }
Then you'll be done :)
@JonB : Since expand slot (http://doc.qt.io/qt-5/qtreeview.html#expand) isn't virtual , how i override it ??
-
-
@JonB : Since expand slot (http://doc.qt.io/qt-5/qtreeview.html#expand) isn't virtual , how i override it ??
@Vinoth-Rajendran4
As I said I'm not C++, so I'm afraid that is above my pay grade.If you can't do that, you might have to do something like intercept the clicks yourself, or can you "disable" an individual item, or maybe you can even derive from
QFileSystemModel
for your model and have that pretend that current directory has no children at all? These are just ideas....Edit Here's a thought: can you use http://doc.qt.io/qt-5/qfilesystemmodel.html#flags to clear the http://doc.qt.io/qt-5/qt.html
Qt::ItemIsEnabled
flag/setDisabled on the current directory in the model? Would that stop user expanding it? -
Hi
There is also
http://doc.qt.io/qt-5/qtreeview.html#itemsExpandable-prop -
Hi
There is also
http://doc.qt.io/qt-5/qtreeview.html#itemsExpandable-prop -
@JonB
Ahh, i missed that. sounded like he wanted for all view.
TreeWidget has setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
so i assume there is a model alternative to that.
Im not sure if it actually disabled expanding or if its just cosmetic . -
This post is deleted!
-
@Vinoth-Rajendran4
As I said I'm not C++, so I'm afraid that is above my pay grade.If you can't do that, you might have to do something like intercept the clicks yourself, or can you "disable" an individual item, or maybe you can even derive from
QFileSystemModel
for your model and have that pretend that current directory has no children at all? These are just ideas....Edit Here's a thought: can you use http://doc.qt.io/qt-5/qfilesystemmodel.html#flags to clear the http://doc.qt.io/qt-5/qt.html
Qt::ItemIsEnabled
flag/setDisabled on the current directory in the model? Would that stop user expanding it?This post is deleted! -
@Vinoth-Rajendran4
As I said I'm not C++, so I'm afraid that is above my pay grade.If you can't do that, you might have to do something like intercept the clicks yourself, or can you "disable" an individual item, or maybe you can even derive from
QFileSystemModel
for your model and have that pretend that current directory has no children at all? These are just ideas....Edit Here's a thought: can you use http://doc.qt.io/qt-5/qfilesystemmodel.html#flags to clear the http://doc.qt.io/qt-5/qt.html
Qt::ItemIsEnabled
flag/setDisabled on the current directory in the model? Would that stop user expanding it?@JonB said in How to control collapse/expand in QTreeView:
, or maybe you can even derive from QFileSystemModel for your model and have that pretend that current directory has no children at all?
found the solution with https://stackoverflow.com/a/18007243 , just as your idea :)