[PyQt] Prevent loading column when checkstate alone changed in QColumnView

Rathinam Signup tfa.signup.test at gmail.com
Thu Feb 23 10:45:03 GMT 2017


I've created a columnview with checkboxes. However whenever I check or
uncheck an item, next column is loaded automatically. I don't want to load
the next column when the checkbox state changed. I want it to be shown only
when the particular row is selected/clicked.

In my program I implemented QAbstractItemModel. For an example I'm using
QFileSystemModel here

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)
>
>         self.ui.columnView.setRootIndex(self.model.index(path))
>
>         self.show()
>
> 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_())


Any help is appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170223/a215e63f/attachment.html>


More information about the PyQt mailing list