Hi all,

I'm writing a simple table view to display a selection of files available in a repository for the user to select for download.
Using the model/view approach I have set the item to isCheckable(True).
The one tweak in behaviour I am trying to introduce is that I'd like the checkboxes to auto-adjust according to the respective rows selection state, so that the user can drag over a bunch of rows to check the boxes, rather than having to click multiple times. So I implemented the view's selectionChanged() virtual function to drive the checkboxes for the selected/deselected items.

This works perfectly fine, the only hiccup I'm seeing is that when clicking on an unchecked checkbox directly, it won't be checked the first time, only the second time, when the respective row is selected as well, will the click cause the checkbox to be checked.
Interestingly, the culprit seems to be this block in selectionChanged():
        for i in selected.indexes():
# this causes checkboxes to not be checked unless the respective row is selected
            item = self.model().itemFromIndex(i)
            item.setCheckState(Qt.Checked)


How can I fix this little glitch? Or should I be doing this in an entirely different way?
Below is the entire test code (raw and in pastebin link).

Thanks in advance for any advise,
frank




http://pastebin.com/T3tSgWRr


from PySide.QtGui import *
from PySide.QtCore import *


class FileModel(QStandardItemModel):

    def __init__(self, toolData, parent=None):
        super(FileModel, self).__init__(parent)
        self.containers = []
        self.headerLabels = ['file name']
        self.dataDict = toolData
        self.setUp()

    def setUp(self):
        '''fill model with data'''

        self.clear()
        for row, file_ in enumerate(self.dataDict['files']):
            fileItem = QStandardItem(file_['filename'])
            fileItem.setCheckable(True)
            font = QApplication.font()
            font.setPointSize(10)
            fileItem.setFont(font)
            self.setItem(row, 0, fileItem)

        self.setHorizontalHeaderLabels(self.headerLabels)


class FileChoserView(QTableView):

    def __init__(self, parent=None):
        super(FileChoserView, self).__init__(parent)
        self.setSortingEnabled(True)
        self.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.horizontalHeader().setStretchLastSection(True)
        self.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.setContextMenuPolicy(Qt.ActionsContextMenu)
        horizontalHeader = self.horizontalHeader()
        self.verticalHeader().hide()

    def selectionChanged(self, selected, deselected):
        '''Check item when row is selected'''

        for i in selected.indexes():
# this causes checkboxes to not be checked unless the respective row is selected
            self.model().itemFromIndex(i).setCheckState(Qt.Checked)

        for i in deselected.indexes():
self.model().itemFromIndex(i).setCheckState(Qt.Unchecked)


if __name__ == '__main__':
    import sys
    app = QApplication([])
testData = {'files':[dict(filename='file_{}.zip'.format(i)) for i in xrange(10)]}

    w = FileChoserView()
    model = FileModel(testData)
    w.setModel(model)
    w.show()
    sys.exit(app.exec_())



--
ohufxLogo 50x50 <http://www.ohufx.com> *vfx compositing <http://ohufx.com/index.php/vfx-compositing> | *workflow customisation and consulting <http://ohufx.com/index.php/vfx-customising>* *

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to