1

我不熟悉 PyQt 和 QColumnView,我该如何做才能获得用户选择的任何内容的“描述”,而不是我可以单击扩展列视图的另一行项目?

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"))

这就是我希望它出现的方式

4

1 回答 1

1

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_())
于 2020-03-09T21:16:49.380 回答