Results 1 to 7 of 7

Thread: QListWidgetItem->text() causing application segfault

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Wink QListWidgetItem->text() causing application segfault

    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?
    Last edited by Valheru; 21st August 2006 at 13:12.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidgetItem->text() causing application segfault

    every time I try and access the current selected items text using QListWidgetItem->currentItem()->text()
    Shouldn't it be QListWidget rather than QListWidgetItem

  3. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListWidgetItem->text() causing application segfault

    Quote Originally Posted by munna
    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidgetItem->text() causing application segfault

    You should always check the returned pointer as it can be 0 if there is no current item.
    J-P Nurmi

  5. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListWidgetItem->text() causing application segfault

    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.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidgetItem->text() causing application segfault

    Yes, that's the problem. "Current item" is the item having the keyboard focus. See this post for explanation.
    J-P Nurmi

  7. #7
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListWidgetItem->text() causing application segfault

    Quote Originally Posted by jpn
    Yes, that's the problem. "Current item" is the item having the keyboard focus. See this post 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

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.