PDA

View Full Version : The order of items in QTreeWidget



KK
26th February 2009, 12:41
Hi, all:)
I'm newbe in this forum.

I want to know how to put QTreeWidgetItem in QTreeWidget from bottom up.
It means first added item is located in the bottom row and last added item is located in the top row.
I tried to use treeWidget->insertTopLevelItem(0,item);,
but it doesn't work.

Anyone has good idea?

Thank you in advance.:cool:

aamer4yu
26th February 2009, 13:06
Use QTreeWidget::insertTopLevelItem(0,pTreeWidgetItem) ;
It will add ur items on top, and the top will move below :)

Edit : Sorry, didnt pay attention that you tried it already. Can u show us the code that you have tried ? It should work I geuss.

KK
27th February 2009, 05:01
aamer4yu, Thank you for your reply.

My app is a little bit complex.
So I created a simple code.
Here is the code.


void TreeWidgetTest::addItem(){
QTreeWidgetItem *item = new QTreeWidgetItem(ui.treeWidget);
item->setText(0,ui.lineEdit->text());
ui.treeWidget->insertTopLevelItem(0,item);
ui.lineEdit->setText("");
}

addItem() function is called when a pushButton is pressed.
This function adds the text is filled in lineEdit into treeWidget.
As you can see, "QTreeWidget::insertTopLevelItem" is used.
But text is inserted below the text which was already added.

samething is wrong?:(

aamer4yu
27th February 2009, 05:25
samething is wrong?
Yesss... something is wrong...
even I tried your code and got the same behaviour... then traced the program and finally found the culprit...

QTreeWidgetItem *item = new QTreeWidgetItem(ui.treeWidget);
You are passing tree widget as parent... and according to the docs...

void QTreeWidget::insertTopLevelItem ( int index, QTreeWidgetItem * item )
Inserts the item at index in the top level in the view.
If the item has already been inserted somewhere else it wont be inserted.

So when u try ui.treeWidget->insertTopLevelItem(0,item);, it sees that it has already been added to the treewidget, see ctor of tree widget item...
Just replace that line with this -

QTreeWidgetItem *item = new QTreeWidgetItem;

and u get ur desired flow :)

KK
27th February 2009, 05:54
Thanks for your quick reply!



QTreeWidgetItem *item = new QTreeWidgetItem(ui.treeWidget);
You are passing tree widget as parent... and according to the docs...


I see!:)
Oh! It was very easy stuff...
I could't notice that.

Thank you so much!