在QColumnView中的start处加载两列

时间:2017-02-24 09:45:00

标签: qt pyqt

有没有办法在QColumnView中启动时加载多个列?

我尝试在树视图中模拟所需索引的点击。虽然收到了click事件,但它不会加载第二列。尝试使用索引调用createColumn。但这两种方法都不起作用。

from PyQt4 import QtCore, QtGui
import os

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class MyModel(QtGui.QFileSystemModel):
    def __init__(self):
        super().__init__()
        self.checkedIndexes = {}
        self.parentChecked=False

    def flags(self,index):
        flags=super().flags(index)|QtCore.Qt.ItemIsUserCheckable
        return flags

    def checkState(self, index):
        if index in self.checkedIndexes:
            return self.checkedIndexes[index]
        else:
            return QtCore.Qt.Checked

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.CheckStateRole:
            if index.column() == 0:
                return self.checkState(index)
        else:
            return super().data(index, role)

    def setData(self, index, value, role):
        if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
            self.checkedIndexes[index] = value
            self.dataChanged.emit(index,index)
            return True

        return super().setData(index, value, role)

    def hasChildren(self,index):
        hasChildren=super().hasChildren(index)
        path=super().filePath(index)

        dirIter=QtCore.QDirIterator(path,QtCore.QDir.AllDirs|QtCore.QDir.NoDotAndDotDot|QtCore.QDir.NoSymLinks)
        if dirIter.hasNext():
            return True
        else:
            return False

        return hasChildren

class columnView(QtGui.QDialog):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.model=MyModel()
        self.model.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.NoDotAndDotDot|QtCore.QDir.NoSymLinks)
        path=os.path.expanduser("~")
        self.model.setRootPath(path)
        self.ui.columnView.setModel(self.model)
        #print("path=",path)
        self.ui.columnView.setRootIndex(self.model.index(path))

        self.ui.columnView.updatePreviewWidget.connect(self.closePreview)

        self.show()
        openIndex=self.model.index(os.path.join(path,"Documents"))
        self.ui.columnView.createColumn(openIndex)
        #QtCore.QMetaObject.invokeMethod(self.ui.columnView, "clicked", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(QtCore.QModelIndex, openIndex))
        self.ui.columnView.clicked.connect(self.rowClicked)
        self.ui.closePushButton.clicked.connect(self.close)

    def rowClicked(self,index):
        print("row clicked=",self.model.filePath(index))

    def closePreview(self,index):
        self.ui.columnView.setPreviewWidget(None)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(596, 389)
        self.verticalLayout = QtGui.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.columnView = QtGui.QColumnView(Dialog)
        self.columnView.setObjectName(_fromUtf8("columnView"))
        self.verticalLayout.addWidget(self.columnView)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.closePushButton = QtGui.QPushButton(Dialog)
        self.closePushButton.setObjectName(_fromUtf8("closePushButton"))
        self.horizontalLayout.addWidget(self.closePushButton)
        self.verticalLayout.addLayout(self.horizontalLayout)

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.closePushButton.setText(_translate("Dialog", "Close", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    view = columnView()
    sys.exit(app.exec_())

虽然TreeView和ColumnView都设计用于显示分层数据,但我觉得与TreeView相比,ColumnView实现的重要性较低且非常令人沮丧。在TreeView中,您可以使用QTreeView.expand(index)轻松完成上述操作。

1 个答案:

答案 0 :(得分:0)

唯一的方法是使用selection model

选择带索引的行
self.ui.columnView.selectionModel().setCurrentIndex(index,QtGui.QItemSelectionModel.Current|QtGui.QItemSelectionModel.Select)

这将突出显示该行,并将加载相应的下一列。

参考:https://forum.qt.io/topic/76588/loading-two-columns-at-start-in-qcolumnview