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. How to set keyboard focus to a QListWidgetItem?
Forum Updated to NodeBB v4.3 + New Features

How to set keyboard focus to a QListWidgetItem?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.9k Views 2 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
    D.Cent
    wrote on last edited by
    #1

    So I wrote a mobile car player in C++ using Qt5. The program mainly consists of a QListWidget. You click on a song and it plays. The song that is currently playing should always be selected automatically.

    So far everything works great - the QListWidget is set to only have one item selected at once. Now if you play a song and the song ended, the next one in the list gets selected automatically. This works as intended, too (using setSelected() on QListWidgetItem). However, once the selected item gets changed programatically, there is still a keyboard focus set on the item which has been clicked previously. I can see this by moving up and down using the arrow keys on my keyboard - there's also a thin blue line under the QListWidgetItem which currently has the keyboard focus.

    Usually, there is a setFocus() function for each widget, but since QListWIdgetItems are no widgets (which makes sense), there is only a setFocus() function for the QListWidget - but it can't set the keyboard focus to a specific item.

    Is there any way to manipulate the keyboard focus?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      HI and welcome
      Did you try
      and set
      setCurrentRow(int row)
      instead?
      maybe with
      row(const QListWidgetItem *item)
      http://doc.qt.io/qt-5/qlistwidget.html#row

      if you already have the QListWidgetItem
      widget->setCurrentRow(widget->row(item));

      D 1 Reply Last reply
      2
      • mrjjM mrjj

        HI and welcome
        Did you try
        and set
        setCurrentRow(int row)
        instead?
        maybe with
        row(const QListWidgetItem *item)
        http://doc.qt.io/qt-5/qlistwidget.html#row

        if you already have the QListWidgetItem
        widget->setCurrentRow(widget->row(item));

        D Offline
        D Offline
        D.Cent
        wrote on last edited by
        #3

        @mrjj Wow thank you - that did the trick :)

        1 Reply Last reply
        1

        • Login

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