QListWidget, no item was selected - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: QListWidget, no item was selected (/thread-40467.html) |
QListWidget, no item was selected - flash77 - Aug-01-2023 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... RE: QListWidget, no item was selected - Axel_Erfurt - Aug-01-2023 try amount_of_selected_items = len(self.listbox_profile.selectedItems())
RE: QListWidget, no item was selected - deanhystad - Aug-02-2023 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") RE: QListWidget, no item was selected - flash77 - Aug-02-2023 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... RE: QListWidget, no item was selected - Axel_Erfurt - Aug-02-2023 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())
|