[SOLVED]How to get a selected item from QListWidget ?



  • How can i access the selected QListWidget item and grab its string ?



  • Did you try using QListWidget::selectedItems()? That gives you access to the currently selected item or items. Note that this is different from the current item, which you can access via currentItem()

    The QListWidgetItem has a text() method you should check out.



  • [quote author="Andre" date="1333563945"]Did you try using QListWidget::selectedItems()? That gives you access to the currently selected item or items. Note that this is different from the current item, which you can access via currentItem()

    The QListWidgetItem has a text() method you should check out. [/quote]

    Thank you very much for the hints.I got it to work.

    The final solution was :
    [code]
    const QString& s = ui->listWidget->currentItem()->text();
    [/code]



  • One tip: check the return value of currentItem() before you dereference it! If you happen to run this code at a moment you don't have a current item, your application will either crash or, worse, start to behave in other unexpected ways that may damage your data...



  • Done.
    Thanks again!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.