Results 1 to 6 of 6

Thread: QListWidgetItem row id

  1. #1
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question QListWidgetItem row id

    Hello, i recently made this same exact post over at another Qt based forum, but went 7 hours without a response. In my opinion this is far to long for what seems to be a simple question, so i decided to give this forum a shot instead. Dont get my wrong, im fine with a little wait, but 7 hours for this particular question is unacceptable in my eyes (especially since it was my first post).

    Im gonna quote my old topic instead of re-writting it. Hopefully you guys will be of more help (this community certainly does give off a more professional aura).

    First of all, hello everyone . I just registered here! Im very excited to have other Qt lovers around to learn from (im still a bit new to Qt, so there's gonna be a lot of questions aha).

    Anyways Is there anyway to get a QListWidgetItem's row value? I need its place in the List, but i dont want to do a pointless loop like:

    Qt Code:
    1. QListWidgetItem *selected = m_Formats->selectedItems().first();
    2. for(int i = 0, l = m_Formats->count(); i < l; i++)
    3. {
    4. if(m_Formats->item(i)->text() == selected->text())
    5. {
    6. //i = selected's index pos!
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    if i dont have to. Is there a member function that i might've missed that does this?
    Thanks!

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QListWidgetItem row id

    Qt Code:
    1. QListWidgetItem *selected = m_Formats->selectedItems().first();
    2. int row = m_Formats->row(selected);
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to nix for this useful post:

    Kyosaur (24th October 2011), rqi115 (25th October 2013)

  4. #3
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidgetItem row id

    You are awesome nix! Thanks a lot man, this was exactly what i was looking for, it even solved a crashing issue i was having lol.

    +Rep and "+thanks" lol.

  5. #4
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidgetItem row id

    sorry.. i'm newbie on qt and c++, what's the meaning about "m_formats"?

    thats QString or something? hehe , thank you for your attention

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QListWidgetItem row id

    "m_Formats" is the name of a QListWidget instance in the OP's class. The "m_" prefix is a frequently used naming scheme for member variables.


    Both the OP's code and nix's code assume that the list widget has one or more selected items. This is not guaranteed to be the case in general and calling first() on an empty list is a ticket to undefined behaviour.

  7. The following user says thank you to ChrisW67 for this useful post:

    rqi115 (25th October 2013)

  8. #6
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidgetItem row id

    I got it, thank you ChrisW67

Similar Threads

  1. Customize QListWidgetItem, how to?
    By martinn in forum Qt Programming
    Replies: 24
    Last Post: 12th December 2016, 10:47
  2. QListWidget and QListWidgetItem
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 3rd August 2011, 09:59
  3. Replies: 4
    Last Post: 4th August 2010, 20:48
  4. about QListWidgetItem
    By Pang in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2007, 09:14
  5. QListWidget add QListWidgetItem
    By fellobo in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2006, 19:37

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.