Results 1 to 14 of 14

Thread: Adding strings in QTreeWidget ?

  1. #1
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Adding strings in QTreeWidget ?

    Hi All,

    I am using QTreeWidget class to display the datas in tree.

    Having list of qstrings which are stored in qstringlist, how can i move these strings to first column of qtreewidget ? and also i want to display those items with selection boxes which provide multiple selection.

    thanks in advance,

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adding strings in QTreeWidget ?

    What have you tried so far? Did you notice the example in the QTreeWidget docs?

    1. Use QTreeWidget::setColumnCount() to set sufficient amount of columns
    2. iterate the list of strings, create a QTreeWidgetItem per each
    3. use QTreeWidgetItem::setFlags() to set the item checkable
    4. use QTreeWidgetItem::setCheckState() to initialize the check state
    J-P Nurmi

  3. #3
    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: Adding strings in QTreeWidget ?

    and also QAbstractItemView::SelectionMode

  4. #4
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Adding strings in QTreeWidget ?

    Thanks JPN and aamer4yu,
    Now I'm able to get the texts in tree widget.

    actually each item should preceded by check box, for the user selection.
    tried ItemIsUserCheckable and other flags in the setflag option, not working.

    how to display each item with check box ?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adding strings in QTreeWidget ?

    Just follow the steps above.. Did you initialize the check state?
    Qt Code:
    1. item->setFlags(item->flags() | Qt::ItemIsUserCheckable); // default flags + checkable
    2. item->setCheckState(0, Qt::Unchecked); // you must also initialize the state
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    npc (24th January 2007)

  7. #6
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding strings in QTreeWidget ?

    Could you tell how to get selected items into a list, from a treewidget ?.

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adding strings in QTreeWidget ?

    Quote Originally Posted by npc View Post
    Could you tell how to get selected items into a list, from a treewidget ?.
    Come on! QTreeWidget::selectedItems()
    J-P Nurmi

  9. #8
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding strings in QTreeWidget ?

    sorry, I forgot to tell that, I used that function... it always retuns the count of 1.
    the list is in a multiple selection mode.

    Qt Code:
    1. QList<QTreeWidgetItem *> selectedGrItems = ui.GroupsTreeWidget->selectedItems();
    2. int gcount = selectedGroupItems.count ();
    To copy to clipboard, switch view to plain text mode 

    and I used the following code to get the item text.

    Qt Code:
    1. QTreeWidgetItem it(selectedGrItems.at(0));
    2. QString text = it.text(1);
    To copy to clipboard, switch view to plain text mode 

    but it is not getting the text

    plz tell me how to get those items in a qstringlist ?

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adding strings in QTreeWidget ?

    Qt Code:
    1. QTreeWidgetItem* it = selectedGrItems.at(0);
    2. QString text = it->text(1);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. #10
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Adding strings in QTreeWidget ?

    Hi,

    When I tried to get all selected item in selectedGrItems, its always returns only single item, i.e; the gcount is 1 always.

    Qt Code:
    1. QList<QTreeWidgetItem *> selectedGrItems = ui.GroupsTreeWidget->selectedItems();
    2. int gcount = selectedGroupItems.count ();
    To copy to clipboard, switch view to plain text mode 

    How to get a whole list of selected items ?

  12. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adding strings in QTreeWidget ?

    Quote Originally Posted by npc View Post
    Hi,

    When I tried to get all selected item in selectedGrItems, its always returns only single item, i.e; the gcount is 1 always.

    Qt Code:
    1. QList<QTreeWidgetItem *> selectedGrItems = ui.GroupsTreeWidget->selectedItems();
    2. int gcount = selectedGroupItems.count ();
    To copy to clipboard, switch view to plain text mode 

    How to get a whole list of selected items ?
    Hi

    A QTreeWidgetItem represents the whole row. Do you have several rows selected? Perhaps you want the text from different columns?

    Qt Code:
    1. QString col1 = item->text(0);
    2. QString col2 = item->text(1);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  13. #12
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding strings in QTreeWidget ?

    Hi

    A QTreeWidgetItem represents the whole row. Do you have several rows selected? Perhaps you want the text from different columns?
    Yes, I have several rows selected. In my code the QTreeWidgetItem has the item which is selected last.

    actually i used the following code to add the items into the the QTreeWidget

    Qt Code:
    1. QList<QTreeWidgetItem *> groupItems;
    2.  
    3. for (int i = 0; i < gName.count(); ++i)
    4. {
    5. n = new QTreeWidgetItem((QTreeWidget*)0, QStringList(gName.at(i)));
    6. n->setFlags( n->flags() | Qt::ItemIsUserCheckable );
    7. n->setCheckState(0,Qt::Unchecked);
    8. items.append(n);
    9. }
    10.  
    11. int count = groupItems.count ();
    12.  
    13. ui.GroupsTreeWidget->insertTopLevelItems(0, groupItems);
    To copy to clipboard, switch view to plain text mode 

    so i want to get the selected rows in a list

    help me in this regard.

  14. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adding strings in QTreeWidget ?

    I see, the items are set as checkable. So just to make sure, are we talking about "checked" or "selected" items?

    For checked items you can use QTreeWidgetItemIterator:
    Qt Code:
    1. while (*it)
    2. {
    3. QString col0 = (*it)->text(0);
    4. ...
    5.  
    6. ++it;
    7. }
    To copy to clipboard, switch view to plain text mode 

    For selected items, try playing around with this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TreeWidget : public QTreeWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. TreeWidget(QWidget* parent = 0) : QTreeWidget(parent)
    9. {
    10. connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(printSelection()));
    11. }
    12.  
    13. private slots:
    14. void printSelection()
    15. {
    16. qDebug() << "selection:";
    17. foreach (QTreeWidgetItem* item, selectedItems())
    18. {
    19. for (int col = 0; col < item->columnCount(); ++col)
    20. {
    21. row += item->text(col);
    22. }
    23. qDebug() << "row:" << row.join(", ");
    24. }
    25. }
    26. };
    27.  
    28. int main(int argc, char *argv[])
    29. {
    30. QApplication a(argc, argv);
    31. TreeWidget treeWidget;
    32. treeWidget.setColumnCount(2);
    33. treeWidget.setSelectionMode(QAbstractItemView::ExtendedSelection);
    34. for (int i = 0; i < 10; ++i)
    35. new QTreeWidgetItem(&treeWidget, QStringList() << QString("item %1 col 0").arg(i) << QString("item %1 col 1").arg(i));
    36. treeWidget.show();
    37. return a.exec();
    38. }
    39.  
    40. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  15. The following user says thank you to jpn for this useful post:

    npc (30th January 2007)

  16. #14
    Join Date
    Mar 2006
    Posts
    53
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding strings in QTreeWidget ?

    oops I confused with checked items and selected items, got it right now.

    I am doing with checked items.

Similar Threads

  1. Adding buttons on the tab part of a tabwidget
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 20th December 2006, 17:52
  2. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32
  3. QTreeWidget & QListWidget different selection
    By munna in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2006, 06:50
  4. How to capture resizing of QTreeWidget columns?
    By simk in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 06:10
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32

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.