Results 1 to 14 of 14

Thread: Adding strings in QTreeWidget ?

Hybrid View

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

    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

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

    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.

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

    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

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

    npc (30th January 2007)

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

    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, 18:52
  2. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 23:32
  3. QTreeWidget & QListWidget different selection
    By munna in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2006, 07:50
  4. How to capture resizing of QTreeWidget columns?
    By simk in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 07:10
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 08: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
  •  
Qt is a trademark of The Qt Company.