pythonpyqtpyqt5qstandarditemqcolumnview

How to show description of item selected in column view?


I'm unfamiliar with PyQt and QColumnView, how do I make it so that we get a "description" of whatever the user selects and not another row of items that I can click on extending the column view?

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(423, 390)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.add_btn = QtWidgets.QPushButton(self.centralwidget)
        self.add_btn.setGeometry(QtCore.QRect(334, 345, 75, 23))
        self.add_btn.setObjectName("add_btn")
        self.auto_btn = QtWidgets.QPushButton(self.centralwidget)
        self.auto_btn.setGeometry(QtCore.QRect(245, 345, 81, 23))
        self.auto_btn.setObjectName("auto_btn")
        self.auto_num = QtWidgets.QLineEdit(self.centralwidget)
        self.auto_num.setGeometry(QtCore.QRect(141, 346, 99, 20))
        self.auto_num.setText("")
        self.auto_num.setClearButtonEnabled(False)
        self.auto_num.setObjectName("auto_num")
        self.columnView = QtWidgets.QColumnView(self.centralwidget)
        self.columnView.setGeometry(QtCore.QRect(10, 10, 399, 320))
        self.columnView.setObjectName("columnView")
        model = QtGui.QStandardItemModel()
        for i in 'abcd':
            group = QtGui.QStandardItem(i)
            model.appendRow(group)
        self.columnView.setModel(model)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.add_btn.setText(_translate("MainWindow", "Add Item"))
        self.auto_btn.setText(_translate("MainWindow", "Auto-populate"))

this is how i want it to appear


Solution

  • I don't need to use QColumnView, just QListView + QTextEdit and associate a role for the description, so when the selection changes, change the text shown in the QTextEdit.

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    DescriptionRole = QtCore.Qt.UserRole + 1000
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.listview = QtWidgets.QListView()
            self.textedit = QtWidgets.QTextEdit(readOnly=True)
    
            self.model = QtGui.QStandardItemModel()
            self.listview.setModel(self.model)
    
            data = [
                ("a", "description of 'a'"),
                ("b", "description of 'b'"),
                ("c", "description of 'c'"),
                ("d", "description of 'd'"),
            ]
    
            for text, description in data:
                group = QtGui.QStandardItem(text)
                group.setData(description, DescriptionRole)
                self.model.appendRow(group)
    
            self.listview.selectionModel().selectionChanged.connect(
                self.on_selection_changed
            )
    
            central_widget = QtWidgets.QWidget()
            self.setCentralWidget(central_widget)
            hlay = QtWidgets.QHBoxLayout(central_widget)
            hlay.addWidget(self.listview)
            hlay.addWidget(self.textedit)
    
        @QtCore.pyqtSlot()
        def on_selection_changed(self):
            indexes = self.listview.selectedIndexes()
            if indexes:
                index = indexes[0]
                description = index.data(DescriptionRole)
                self.textedit.setPlainText(description)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())