Results 1 to 5 of 5

Thread: The order of items in QTreeWidget

  1. #1
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy The order of items in QTreeWidget

    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.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The order of items in QTreeWidget

    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.

  3. #3
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: The order of items in QTreeWidget

    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:
    1. void TreeWidgetTest::addItem(){
    2. QTreeWidgetItem *item = new QTreeWidgetItem(ui.treeWidget);
    3. item->setText(0,ui.lineEdit->text());
    4. ui.treeWidget->insertTopLevelItem(0,item);
    5. ui.lineEdit->setText("");
    6. }
    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?

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: The order of items in QTreeWidget

    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

  5. The following user says thank you to aamer4yu for this useful post:

    KK (27th February 2009)

  6. #5
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up [solved] The order of items in QTreeWidget

    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!
    Last edited by KK; 27th February 2009 at 05:01.

Similar Threads

  1. (PyQt) moving items within a QTreeWidget
    By calireno in forum Newbie
    Replies: 0
    Last Post: 12th November 2008, 03:33
  2. amount of items in QTreeWidget
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2008, 22:38
  3. QTreeWidget - locating items in viewport
    By kemp in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2007, 15:18
  4. Saving/Restoring columns order of QTreeWidget
    By mchara in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 08:43
  5. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 12:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.