PDA

View Full Version : Another simple question...



Dark_Tower
20th March 2006, 13:06
Hi everybody, I'm new on Qt and I want to know what's the common/elegant way to program this: I have a window and it manages a QTreeWidget and also another widgets. The tree widget is a tree of directories to know in wich directory have to work the other widgets . I want that when the user expand or collapse a directory, its item shows an opened or closed folder icon. I also want that, when the user expands a directory, create its subdirectories (if they aren't created yet). Well, as you can see this tree widget has a particular behaviour, but it doesn't need any private variable and also I only want an instance of this tree widget. I want to know if I should subclass QTreeWidget (and put in this new class all the SIGNALS and SLOTS to make the tree working as I want) or put all the code to manage this tree widget in the code of the window although it's specific for only the tree?

Thanks.

Xagen
20th March 2006, 13:55
This is code from my small program:


QStyle *style = tableView->style();

m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
QIcon::Normal, QIcon::Off);
m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
QIcon::Normal, QIcon::On);

//here we get the folderIcon.

QTreeWidgetItem *newItem = new QTreeWidgetItem(tableView);
newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
newItem->setIcon(0, folderIcon());
newItem->setText(0, text);

//tags
newItem->setData(0, Qt::UserRole, "folder");

And also make this item expandable!

This is only part of code, use it as example and you'll understand.

Dark_Tower
20th March 2006, 15:04
Thanks Xagen for the code, but I what want to know is if the common way to manage this tree widget is to subclass it (to relase code from the main window) or put the connections with the signals (item expanded/colapsed) and the code for its slots in the main window?

jpn
20th March 2006, 15:21
Thanks Xagen for the code, but I what want to know is if the common way to manage this tree widget is to subclass it (to relase code from the main window) or put the connections with the signals (item expanded/colapsed) and the code for its slots in the main window?
It's totally up to you. Mostly the only compulsion for subclassing is when you need to access some protected methods. Thanks to Qt's event filters, you can even catch events without subclassing a widget. But of course it depends on many factors. For example the amount of widget specific code you would need to write in another class. I would avoid writing exceeding amount of widget specific code in another class, just for the sake of clarity. And in an opposed situation it could be a bit vain to subclass just to add a simple feature/functionality when you can achive the same "outside" the class..

Dark_Tower
25th March 2006, 11:48
Hi, sorry but I have to re-open this post. This time I need to do some questions about the code that Xagen kindly posted previously in this post:

1) Can Xagen or someone else please explain me why an assignation of a pixmap in the same icon 2 times?


m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon),
QIcon::Normal, QIcon::Off);
m_folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon),
QIcon::Normal, QIcon::On);

2) The method "folderIcon" which of both pixmaps will contain the returned icon? And why doesn't access directly to the variable "m_folderIcon" ? as seems that's the purpose...

newItem->setIcon(0, folderIcon());

Thanks.

Xagen
31st March 2006, 17:13
Answer is very simple:

This icon represents two states:
opened

and

closed

----
Sorry for the late answer