PDA

View Full Version : QListWidgetItem->text() causing application segfault



Valheru
21st August 2006, 12:55
I have an instance of a QListWidget in my applications MainWindow. Adding anything to it goes fine, using QListWidget->addItem(const QString&). However, every time I try and access the current selected items text using QListWidget->currentItem()->text(), it causes a segfault. This is on Gentoo Linux, using Qt 4.1 BTW. Is this a (known) bug, or am I making some horrible logic error?

munna
21st August 2006, 13:12
every time I try and access the current selected items text using QListWidgetItem->currentItem()->text()

Shouldn't it be QListWidget rather than QListWidgetItem

Valheru
21st August 2006, 14:12
Shouldn't it be QListWidget rather than QListWidgetItem

Sorry, you're right. That's how it is in my program though, just made a mistake describing it here. Post edited for clarity.

jpn
21st August 2006, 14:21
You should always check the returned pointer as it can be 0 if there is no current item.

Valheru
21st August 2006, 15:11
Maybe I should have been more specific - I check to see that QListWidget->currentRow() is not = -1 before I try and retrieve the text, since QListWidget->currentRow() returns -1 (int) if nothing is selected. And indeed, if there is no entry then the program doesn't segfault. It does segfault when there is a QListWidgetItem in the QListWidget. However, I AM calling the function from a QMenu entry - maybe it is a focus problem? Should I try setting the focus to the QListWidget before calling QListWidget->currentItem()->text()? I ask only because I am at work - I'll give it a shot when I get home. It isn't mentioned in the docs that it needs focus when being called though.

jpn
21st August 2006, 15:21
Yes, that's the problem. "Current item" is the item having the keyboard focus. See this post (http://www.qtcentre.org/forum/f-qt-programming-2/t-what-is-currentitem-668.html#3) for explanation.

Valheru
21st August 2006, 23:08
Yes, that's the problem. "Current item" is the item having the keyboard focus. See this post (http://www.qtcentre.org/forum/f-qt-programming-2/t-what-is-currentitem-668.html#3) for explanation.
Yeah, as soon as I got home I tried it and that fixed it. Never thought my VBA/Access experiance of the last few weeks would have payed off :p