Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to get a selected item from QListWidget ?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 69.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    d777
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        d777
        wrote on last edited by
        #3

        [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]

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • D Offline
            D Offline
            d777
            wrote on last edited by
            #5

            Done.
            Thanks again!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved