PDA

View Full Version : QTreeWidget->clear()?



vishal.chauhan
27th September 2007, 07:24
Hi All,

I m working on Qt 4.1.5 on my MAC.

I clear a QTreeWidget by calling
treeWidget->clear()

Now I want to know that if this function return or emit any signal when the tree is cleared.
so that I can know when that the reeWidget is cleared.


If there is somealternative function which can delete all the treeWidgetItem and return me when this process is complete then plz tell me.

Thanks.

jpn
27th September 2007, 08:59
QTreeWidget::clear() does clear the tree immediately. Do you give it a chance to update itself?

vishal.chauhan
27th September 2007, 10:01
Thanks for reply.

Actually what I want is that I have a LinkList of Items which I m showing in QTreeWidget.
So when I double click a item in the treeWidget I m clearing a tree and showing the child of that QTreeWidgetItem which are in Linked List.


But as QtreeWidget->clear() takes some time to clear a treeWidget if I emit the signal to show its child then it get crashed.

So I want that once the treeWidget is cleared then i emit the signal to add new item on the same tree.

and the application crashes only when i am clearing the treeWidget.

mchara
27th September 2007, 11:21
Aren't you using currentItem(which is NULL after clear()) in method that adds new item?

jpn
27th September 2007, 11:58
To me it sounds like you end up having a bunch of invalid pointers in your external linked list structure. Notice that QTreeWidget::clear() deletes items. Use QTreeWidget::takeTopLevelItem(), QTreeWidgetItem::takeChild() or such for taking items out of QTreeWidget without deleting them.

merry
27th September 2007, 12:51
Hi.......

But If we Use
QTreeWidget::takeTopLevelItem(int index)

then it removes that items only that are inserted at the index we are passing , not all the items that are in the QTreeWidget.

Thanx

jpn
28th September 2007, 07:50
So what's the problem? Take them all one by one:


while (treeWidget->topLevelItemCount() > 0)
treeWidget->takeTopLevelItem(0);


PS. If I were you, I'd rather switch to model-view approach which is way more flexible in situations like this..

merry
28th September 2007, 08:15
Using this , When I double click on treeWidgetItem then also , program crashes


while (treeWidget->topLevelItemCount() > 0)
treeWidget->takeTopLevelItem(0);


Thanx

jpn
28th September 2007, 08:38
So build the app in debug mode, run it via debugger and see the backtrace to see where exactly it crashes. Otherwise all we can do is to keep guessing for the rest of the month.

merry
28th September 2007, 08:46
Thanx for the reply

Yaa u are right ,

Actually , at the time when I double click on the treeWidgetItem to show only its contents and deletes all other , then it Crashes , Otherwise , if i wont clear the treeWidget , then It dont crashes.

There is a problem , in clearing the tree.

Thanx

sabeesh
28th September 2007, 09:13
Hi,
I think you read the data for the tree from a table ( in a database ). is it?
if so, first you create a connection with the tree like this

connect( ManagerTree, SIGNAL( itemDoubleClicked (QTreeWidgetItem*,int) ), this, SLOT( ChkTreeItem(QTreeWidgetItem*, int ) ) );

and you write a function for create the tree passing the frst level of the item.
After that, you create another function named as ChkTreeItem(QTreeWidgetItem*, int ) and in that function you read the text item like this

QString Name = item->text(column);
Then you get the selected item from the variable Name. Using this Variable you can get the level of the item from the table.
Then you clear the tree. after you recall the first function with the level.
I think through this methord you can solve your probs.
Try it..... ;)

jpn
28th September 2007, 09:23
For database data you would use one of available SQL models depending on your needs, and a view:


QSqlQueryModel *model = new QSqlQueryModel(parent);
model->setQuery("SELECT * FROM table");
view->setModel(model);

merry
28th September 2007, 09:45
Thanx for the reply

But I am not using database to store items , I am creating linklist for the items , I also made a connection


connect(TreeWidgetFolder,SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int)), this, SLOT(ShowTreeWidgetFolderItem( QTreeWidgetItem *, int) ));

regards

jpn
28th September 2007, 09:51
Could you paste ShowTreeWidgetFolderItem()? And possibly other relevant code too, for example where you manage the list of items.

merry
28th September 2007, 09:58
Pls preview the code


void FileSystem :: ShowTreeWidgetFolderItem( QTreeWidgetItem *item, int col) // Function call -> itemdoubleClicked
{
bool ok;
if(!bStopFlag)
{
QList<QTreeWidgetItem*>m_list;
m_list = FolderTree->selectedItems();
m_id = m_list[0]->text(1).toInt(&ok,10);

emit ShowFolders(); //emit Signal to show the contents of the item that is double clicked
}
}


void FileSystem :: ShowFolderItem() // function to show the contents of the item that is double clicked

{
TREEITEM *treeitem;

for (int item = 0; item < FolderTree->topLevelItemCount(); ++item)
{
qDeleteAll(FolderTree->topLevelItem(item)->takeChildren());
}
for(unsigned int i = 0;i< list.count();i++)
{
treeitem = list[i];
if(treeitem->MyTreeEntry.dwParentId == m_id)
{
QTreeWidgetItem *RightTreeItem = new QTreeWidgetItem(FolderTree);
RightTreeItem->setText(0,treeitem->bFileName);
RightTreeItem->setCheckState(0,Qt::Unchecked);
RightTreeItem->setText(0,treeitem->bFileName);
RightTreeItem->setText(1,QString::number(treeitem->MyTreeEntry.dwFileId));
RightTreeItem->setText(2,QString::number(treeitem->MyTreeEntry.dwParentId));

if(treeitem->MyTreeEntry.bAttribute == 0x10)
RightTreeItem->setText(3,">");

RightTreeItem->setCheckState(0,Qt::Unchecked);
FolderTree->resizeColumnToContents (0);
FolderTree->resizeColumnToContents (1);
FolderTree->resizeColumnToContents (2);
}
}
}



Thanx

jpn
28th September 2007, 10:10
Now I'm even more convinced that you really should consider switching to model-view approach. You shouldn't be replicating the whole file system as QTreeWidgetItems but use QDirModel.

See threads:

Suggestions/Ideas about a file browser (http://www.qtcentre.org/forum/f-qt-programming-2/t-suggestionsideas-about-a-file-browser-1625.html)
QDirModel+QTreeView and checkable items (http://www.qtcentre.org/forum/f-qt-programming-2/t-qdirmodelqtreeview-and-checkable-items-6957.html)

merry
28th September 2007, 10:56
Hi Jpn

I appreciate your suggestion of using "QDirModel" approach but I think this approach is useful for the local fileSystem , It shows all the directories & files of the local filesystem only.

But If I have to view all the directories & files of some other media , say any Removeable Media , then I think this approach is not useful.


Regards

jpn
28th September 2007, 11:27
Actually QDirModel is capable of providing information about removable media and even mapped network drives on Windows.

Alternatively, you could wrap TREEITEM structure into a QAbstractItemModel but it's a bit of work and sure requires some knowledge of Qt's model-view framework.

However, with QTreeWidgetItems you'll just get lost into jungle. QTreeWidget is not intended to and is not really usable for something like what you're doing.

Gekra
28th September 2007, 13:34
In Qt3 it is not allowed to delete items of the QListView while processing the signals for pressing, clicking or doubleClicking. In that case the Application crashes. Maybe this issue is also existing in the Qt4-Versions.

merry
28th September 2007, 13:38
Sorry but I forget to update my profile ,

Right now , I am working on Qt4.1.5

vishal.chauhan
5th October 2007, 07:00
Hi All,

Thanks for yr reply.
But I still not slove this problem.
I cannot used QDirModel approach because i have done enough code without it and can not change my approach.

Actually problem is that when I clear the tree it give me the error

QTreeModel->hasChildren() on MouseDoubleClickEvent.

So i want to ask is there any other alternative from which I can do this.