Results 1 to 5 of 5

Thread: How to get qlistwidget index and item text in one variable?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to get qlistwidget index and item text in one variable?

    I have two list widgets , one has file lists.. and other will show the selected list from first widget when >> button is pressed and it removes the moved item from first list. Now I would like to multi select the files and move it to the second list. Also, there index should be saved with text in a variable. When the selected item
    is removed from second list when << button is pressed. It should place the items to the particular index where the files were originally present in first list before moving to second list..

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to get qlistwidget index and item text in one variable?

    Storing the index is not a good idea and is discouraged to use. Instead hide() the selected items and show() them back and when required.

    Edit: The API for hide/show item is setHidden()
    Last edited by Santosh Reddy; 25th January 2013 at 08:06.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get qlistwidget index and item text in one variable?

    Could you explain my scenario with some codes?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to get qlistwidget index and item text in one variable?

    Look at this
    Qt Code:
    1. class Worker : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Worker(QListWidget * left, QListWidget * right, QObject *parent)
    6. : QObject(parent)
    7. , leftList(left)
    8. , rightList(right){}
    9.  
    10. public slots:
    11. void addItems()
    12. {
    13. QList<QListWidgetItem*> items = leftList->selectedItems();
    14. for(int i = 0; i < items.count(); i++)
    15. {
    16. QListWidgetItem *item = items[i];
    17. item->setHidden(true);
    18. QList<QListWidgetItem*> rItems = rightList->findItems(item->text(), Qt::MatchExactly);
    19. for(int j = 0; j < rItems.count(); j++)
    20. rItems[j]->setHidden(false);
    21. }
    22. }
    23.  
    24. void remItems()
    25. {
    26. QList<QListWidgetItem*> items = rightList->selectedItems();
    27. for(int i = 0; i < items.count(); i++)
    28. {
    29. QListWidgetItem *item = items[i];
    30. item->setHidden(true);
    31. QList<QListWidgetItem*> lItems = leftList->findItems(item->text(), Qt::MatchExactly);
    32. for(int j = 0; j < lItems.count(); j++)
    33. lItems[j]->setHidden(false);
    34. }
    35. }
    36.  
    37. private:
    38. QListWidget * leftList;
    39. QListWidget * rightList;
    40. };
    41.  
    42. int main(int argc, char **argv)
    43. {
    44. QApplication app(argc, argv);
    45.  
    46.  
    47. QGridLayout * layout = new QGridLayout(&w);
    48.  
    49. QListWidget * leftList = new QListWidget(&w);
    50. QListWidget * rightList = new QListWidget(&w);
    51. QPushButton * addButton = new QPushButton(">>", &w);
    52. QPushButton * remButton = new QPushButton("<<", &w);
    53. Worker * worker = new Worker(leftList, rightList, &w);
    54.  
    55. leftList->setSelectionMode(leftList->ExtendedSelection);
    56. rightList->setSelectionMode(leftList->ExtendedSelection);
    57.  
    58. layout->addWidget(leftList, 0, 0, 4, 1);
    59. layout->addWidget(addButton, 1, 1, 1, 1);
    60. layout->addWidget(remButton, 2, 1, 1, 1);
    61. layout->addWidget(rightList, 0, 2, 4, 1);
    62.  
    63. // Add Sample Items
    64. for(int i = 0; i < 15; i++)
    65. {
    66. const QString text = QString("Item - %1").arg(i);
    67. QListWidgetItem * lItem = new QListWidgetItem(text);
    68. QListWidgetItem * rItem = new QListWidgetItem(text);
    69. leftList->addItem(lItem);
    70. rightList->addItem(rItem);
    71. lItem->setHidden(false);
    72. rItem->setHidden(true);
    73. }
    74.  
    75. worker->connect(addButton, SIGNAL(clicked()), SLOT(addItems()));
    76. worker->connect(remButton, SIGNAL(clicked()), SLOT(remItems()));
    77.  
    78. w.setLayout(layout);
    79. w.show();
    80.  
    81. return app.exec();
    82. }
    83.  
    84. #include "Main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Gokulnathvc (25th January 2013)

  6. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get qlistwidget index and item text in one variable?

    Thank you so much.. Got confused with the index values.. This works perfect..

Similar Threads

  1. Replies: 3
    Last Post: 13th April 2011, 06:43
  2. Z-index of an item is only applied to siblings
    By humbleguru in forum Qt Quick
    Replies: 0
    Last Post: 20th January 2011, 18:20
  3. Replies: 2
    Last Post: 4th December 2010, 08:09
  4. Replies: 4
    Last Post: 31st August 2010, 05:42
  5. Replies: 3
    Last Post: 25th July 2008, 14:30

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.