Python Forum
QListWidget, no item was selected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QListWidget, no item was selected
#1
Hi,

if I have a listwidget, is it possible to detect if no item of the listwidget was selected?

I searched the web, but found nothing helpful...

amount_of_selected_items = self.listbox_profile.selectedItems().count()
if amount_of_selected_items == 0:
    QMessageBox("text")
thanks...
Reply
#2
try

amount_of_selected_items = len(self.listbox_profile.selectedItems())
Reply
#3
Do you need to know the number? You don't if you are only checking if there are selected items. You can do this instead.
if not self.listbox_profile.selectedItems():
    QMessageBox("text")
Reply
#4
Hi,
Axel_Erfurt and deanhystad...

Thanks a lot for your answers!!

The solution from Axel_Erfurt works good.

I don't know what I'm doing wrong, but the solution from deanhystad doesn't work in my code...

Never mind...

I wish you a pleasant day!!

Greetings...
Reply
#5
deanhystad's solution also works.

import sys
from PyQt6.QtWidgets import (QApplication, QMainWindow,  QGridLayout, QListWidget)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Test')
        self.setGeometry(100, 100, 400, 100)
        
        self.list_widget = QListWidget()
        self.list_widget.addItems(['Hello', 'World'])
        self.setCentralWidget(self.list_widget)
        
        self.check_selected()
        
        self.list_widget.setCurrentRow(1)
        
        self.check_selected()
        
        self.show()
        
        
    def check_selected(self):
        if not self.list_widget.selectedItems():
            print("nothing selected")
        else:
            print(f"row {self.list_widget.currentRow()} selected")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())
Output:
nothing selected row 1 selected
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get the color name of qlistwidget? flash77 5 1,282 Aug-13-2023, 07:01 PM
Last Post: flash77
  GUI Problem / call another function / fill QListwidget flash77 5 896 Jul-30-2023, 04:29 PM
Last Post: flash77
  [PyQt] populate a QListWidget devilonline 1 1,611 Apr-10-2023, 02:52 AM
Last Post: deanhystad
  [PyQt] How to always select the item in the first column when a row is selected DrakeSoft 1 1,656 Feb-17-2023, 07:43 PM
Last Post: DrakeSoft
Question [PyQt] CSS Styling for a QLabel inside a QListWidget Alfalfa 2 5,162 Nov-30-2020, 02:59 AM
Last Post: Alfalfa
  How to get the selected item from Listbox and convert it to int? Jionni 8 5,171 Feb-17-2020, 11:07 AM
Last Post: Jionni
  Update value selected in option menu when select an item from text box klllmmm 2 5,037 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  Why QListWidget doesn't show new line? AlekseyPython 3 3,384 Feb-05-2019, 02:23 PM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020