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.
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.
Last edited by aamer4yu; 26th February 2009 at 14:26.
aamer4yu, Thank you for your reply.
My app is a little bit complex.
So I created a simple code.
Here is the code.
Qt Code:
void TreeWidgetTest::addItem(){ item->setText(0,ui.lineEdit->text()); ui.treeWidget->insertTopLevelItem(0,item); ui.lineEdit->setText(""); }To copy to clipboard, switch view to plain text mode
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?![]()
Yesss... something is wrong...samething 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...
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...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.
Just replace that line with this -
QTreeWidgetItem *item = new QTreeWidgetItem;
and u get ur desired flow![]()
KK (27th February 2009)
Thanks for your quick reply!
I see!QTreeWidgetItem *item = new QTreeWidgetItem(ui.treeWidget);
You are passing tree widget as parent... and according to the docs...
Oh! It was very easy stuff...
I could't notice that.
Thank you so much!
Last edited by KK; 27th February 2009 at 05:01.
Bookmarks