Results 1 to 9 of 9

Thread: QListWidget selected item Row id.

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default QListWidget selected item Row id.

    Hello i have a small problem... I am populating a QListWidget with some names like name1 to name300, now when i select a few names from this list say from name1 to name5 and name45 to name80 , i need to create a relavent TagId some thing like TAGID_1 for the selected items in ListWidget.. I was thinking if i know the row id's of the selected names in the ListWidget i can append that row id number to the TagId and i could get the right sequence in how i have selected the items in the ListWidget.

    Qt Code:
    1. QList<QListWidgetItem *>item = NewLISTLW->selectedItems(); // NewLISTLW is Obj name for ListWidget 1
    2. for(kfast = 0;kfast<item.count();kfast++)//insert selected items from list to selected another list.
    3. {
    4. QListWidgetItem * item1 = new QListWidgetItem(item[kfast]->text());
    5. SelectedLW->insertItem(kfast,item1); // SelectedLWis Obj name for ListWidget 2
    6. selectedlist.append(SelctdPresParamsLW->item(kfast)->text());
    7. }
    8. for(int k=0; k< selectedlist.count(); k++)
    9. {
    10. tagidnames.append(QString("TAGID_%1").arg(k)); // QVector<QString> selectedlist,tagidnames;
    11. }
    12. qDebug() << tagidnames;
    To copy to clipboard, switch view to plain text mode 
    but here the TAGID is created for how many items are selected not in relation with rowid.
    if i select name1 to name5 and name45 to name50 instead of creating TAGID1 to TAGID5 and TAGID45 to TAGID50 .. i can see from TAGID1 to TAGID10.. how can i use the selected row id to get what i want here.. ?

    thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,710
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget selected item Row id.

    but here the TAGID is created for how many items are selected not in relation with rowid.
    if i select name1 to name5 and name45 to name50 instead of creating TAGID1 to TAGID5 and TAGID45 to TAGID50 .. i can see from TAGID1 to TAGID10..
    This is also what I would expect from your code to do.
    Your 'item' list as as long as the number of selected items, and the index of thelist has nothing to do with the index of your items in the original list from you you have selected the items from.
    You need to retrieve the index of the items from the original list.

    Qt Code:
    1. for(kfast = 0;kfast<item.count();kfast++)//insert selected items from list to selected another list.
    2. {
    3. QListWidgetItem * item1 = new QListWidgetItem(item[kfast]->text());
    4. SelectedLW->insertItem(kfast,item1); // NewLISTLW is Obj name for ListWidget 2
    5. selectedlist.append(SelctdPresParamsLW->item(kfast)->text());
    6.  
    7. tagidnames.append(QString("TAGID_%1").arg(NewLISTLW->row(item1)));
    8. }
    9. //for(int k=0; k< selectedlist.count(); k++)
    10. //{
    11. // tagidnames.append(QString("TAGID_%1").arg(k)); // QVector<QString> //selectedlist,tagidnames;
    12. //}
    To copy to clipboard, switch view to plain text mode 

    Please note I didn't test compile it.
    Last edited by high_flyer; 20th December 2010 at 11:45.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QListWidget selected item Row id.

    Hello sir thank you so much for the reply.. i did compile the code you gave i have one error..
    for the below code
    tagidnames.append(QString("TAGID_%1").arg(NewLISTL W->row(item1)));
    QListWidget::row' : cannot convert parameter 1 from 'QList<T>' to 'const QListWidgetItem *'
    with
    [
    T=QListWidgetItem *
    ]
    thank you

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,710
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget selected item Row id.

    Then cast the item!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QListWidget selected item Row id.

    hello sir can you please show me a small example of how to do it exactly.. i am not very sure with how to cast this is the first time i am doing something like this..

    Thank you

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,710
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget selected item Row id.

    Qt Code:
    1. tagidnames.append(QString("TAGID_%1").arg(NewLISTLW->row((const QListWidgetItem *)item1)));
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QListWidget selected item Row id.

    hello sir i compiled the code but i don't get the desired out put in QDebug..
    this is what i am getting in case i selected name1 to name3 and name5 to name7 ..
    Qt Code:
    1. QVector("TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1")
    To copy to clipboard, switch view to plain text mode 

    sir i read in qt assistant , i think we won't get the actual row value from this

    QList<QListWidgetItem *> QListWidget::selectedItems () const
    Returns a list of all selected items in the list widget
    since i am coping all selected list from the NewLISTLW, can i get the actual index pointer so that i can use that to print the TAGID_ for the relevant selected sequence in the List.
    QList<QListWidgetItem *>item = NewLISTLW->selectedItems();
    thank you
    Last edited by nagabathula; 20th December 2010 at 16:48.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,710
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget selected item Row id.

    Try this:
    Qt Code:
    1. tagidnames.append(QString("TAGID_%1").arg(NewLISTLW->row(item.at(kfast))));
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. The following user says thank you to high_flyer for this useful post:

    nagabathula (20th December 2010)

  10. #9
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QListWidget selected item Row id.

    thank you so much sir.. problem is solved this worked.. i think i understand what we are doing here.. but i din't understand the previous suggestion.
    thanks for your time you helped me solve my problem..

    Regards

Similar Threads

  1. popupmenu for QTreeWidget's item when a particular item selected.
    By vinod sharma in forum Best Practices in Qt Programming
    Replies: 1
    Last Post: 22nd January 2010, 10:45
  2. Replies: 1
    Last Post: 20th January 2010, 08:38
  3. How to set selected item of a QListWidget?
    By Lawand in forum Qt Programming
    Replies: 9
    Last Post: 5th April 2009, 11:23
  4. Replies: 1
    Last Post: 5th June 2008, 14:02
  5. QListWidget/QTreeWidget, etc selected item color
    By Arthur in forum Qt Programming
    Replies: 4
    Last Post: 15th May 2006, 16:50

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.