Dear visitor, welcome to QtForum.org.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
Is there an equivalent of TVIS_EXPANDEDONCE for QTreeWidgetItem & QTreeWidget?
I'm trying out QT as a possible cross platform solution but have been noticing a lot of the GUI related abilities you take for granted in .NET, MFC, Windows SDK, etc seem to be missing. For instance currently I can't seem to figure out how to check if this is the first time a tree item has been expanded within the slot callback without keeping my own collection of flags which is undesirable since items are loaded dynamically and I don't know how many children I will need to load until I actually do it.
Basically I need a QT equivalent of the following:
|
Source code
|
1
|
if( !( GetItemState(pNMTV->itemNew.hItem, TVIS_EXPANDEDONCE) & TVIS_EXPANDEDONCE ) )
|
"Live as if you were to die tomorrow, learn as if you were to live forever" - Gandhi
I personally
never had the need for such a feature, so I haven't looked into it closer.
Maybe you can do something like this:
TreeWidget.h
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class TreeWidget : public QTreeWidget
{
Q_OBJECT
public:
TreeWidget( QWidget *parent = 0 ), QTreeWidget( parent )
{
connect( this, SIGNAL( itemExpanded( QTreeWidgetItem * ) ), this, SLOT( itemExpanded( QTreeWidgetItem * ) ) );
}
QList<QTreeWidgetItem *> getItemsExpandedOnce()
{
QList<QTreeWidgetItem *> items;
foreach( QObject *object, treeWidget->children() )
{
if( object->property( "expandedOnce" ).toBool() )
items << (QTreeWidgetItem *)object;
}
return items;
}
private slots:
void itemExpanded( QTreeWidgetItem *item )
{
item->setProperty( "expandedOnce", true );
}
};
|
TreeWidgetitem.h
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class TreeWidgetItem : public QTreeWidgetItem
{
Q_PROPERTY( bool expandedOnce READ getExpandedOnce WRITE setExpandedOnce )
private:
bool expandedOnce;
public:
bool getExpandedOnce()
{
return expandedOnce
}
void setExpandedOnce( bool value )
{
expandedOnce = value;
}
};
|
Now you can call getExpandedOnce() on an item to check if it was expanded atleast once, and return all items in a QTreeWidget that was expanded atleast once using getItemsExpandedOnce().
This post has been edited 1 times, last edit by "Znurre" (Jun 18th 2010, 1:34pm)
Yup that would work however subclassing and keeping track of flags is what I was trying to avoid but I guess it's the way to go ...
The reason behind needing this is when an item is expanded I want to add the children of that item to the tree. If they were to collapse it and then expand it again I don't want to add the children a second time and checking for duplicate items is even less efficient than expanded flags. The reason I am not pre-loading all parents and children is because we are dealing with millions of equipment items for IT infrastructure ...
"Live as if you were to die tomorrow, learn as if you were to live forever" - Gandhi
Actually upon further review and an attempted implementation subclassing both tree view and tree items is a pain in the butt as well since more things need to be overloaded or rewritten than just the new flag and slot.
Edit: What I ended up doing was removing children recursively on collapse. This actually works out better in application as if another client was running that made changes they could be seen upon re-expanding as it will grab items and populate the tree on every expand now and it also means only visible items are loaded locally in the client.
"Live as if you were to die tomorrow, learn as if you were to live forever" - Gandhi
This post has been edited 1 times, last edit by "ajg_itracs" (Jun 21st 2010, 5:39pm)