PDA

View Full Version : QListWidgetItem row id



Kyosaur
24th October 2011, 10:15
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:



QListWidgetItem *selected = m_Formats->selectedItems().first();
for(int i = 0, l = m_Formats->count(); i < l; i++)
{
if(m_Formats->item(i)->text() == selected->text())
{
//i = selected's index pos!
}
}



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


Thanks!

nix
24th October 2011, 10:34
QListWidgetItem *selected = m_Formats->selectedItems().first();
int row = m_Formats->row(selected);

Kyosaur
24th October 2011, 10:43
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.

rqi115
25th October 2013, 04:49
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 :D

ChrisW67
25th October 2013, 05:14
"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.

rqi115
25th October 2013, 07:39
I got it, thank you ChrisW67 :)