Search code examples
pythonpyqt5qpushbuttonqlistwidget

How to enable button after QListWidget is no longer empty


I would like my button to be disabled until there are items actually in my QListWidget. I have tried if and while statements in my AppDemo class but I feel like it needs to change in the ListBoxWidget class, however the listBoxWidget is already a sub class of the AppDemo. Example Code:

 import sys, os
    from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidget, QListWidgetItem, QPushButton
    from PyQt5.QtCore import Qt, QUrl


class ListBoxWidget(QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAcceptDrops(True)
        self.resize(600, 600)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()

            links = []
            for url in event.mimeData().urls():
                if url.isLocalFile():
                    links.append(str(url.toLocalFile()))
                else:
                    links.append(str(url.toString()))
            self.addItems(links)
        else:
            event.ignore()


class AppDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(1200, 600)

        self.listbox_view = ListBoxWidget(self)

        self.btn = QPushButton('Get Value', self)
        self.btn.setEnabled(False)
        self.btn.setGeometry(850, 400, 200, 50)
        self.btn.clicked.connect(lambda: print(self.getSelectedItem()))

    def getSelectedItem(self):
        item = QListWidgetItem(self.listbox_view.currentItem())
        return item.text()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    demo = AppDemo()
    demo.show()

    sys.exit(app.exec_())

Solution

  • You have to use the signals that are emitted when the number of rows of the model associated with the view changes, those signals must invoke a method that updates the state of the button based on the number of items in the QListWidget:

    class AppDemo(QMainWindow):
        def __init__(self):
            super().__init__()
            self.resize(1200, 600)
    
            self.listbox_view = ListBoxWidget(self)
            self.listbox_view.model().modelReset.connect(self.handle_rows_changed)
            self.listbox_view.model().rowsInserted.connect(self.handle_rows_changed)
            self.listbox_view.model().rowsRemoved.connect(self.handle_rows_changed)
            self.listbox_view.model().layoutChanged.connect(self.handle_rows_changed)
            
            self.btn = QPushButton("Get Value", self)
            self.btn.setEnabled(False)
            self.btn.setGeometry(850, 400, 200, 50)
            self.btn.clicked.connect(lambda: print(self.getSelectedItem()))
    
        def getSelectedItem(self):
            item = self.listbox_view.currentItem()
            return item.text() if item is not None else ""
    
        def handle_rows_changed(self):
            self.btn.setEnabled(bool(self.listbox_view.count()))