[PySide] QTableView - auto check item when row is selecte

Sebastian Elsner sebastian at risefx.com
Sat Jan 23 13:19:06 CET 2016


Hey,

you could use the view's QItemSeletionModel and its "currentChanged" signal.

However, I find this a bit weird from an UX point of view. This is what
the |QAbstractItemView::ExtendedSelection and
||QAbstractItemView::MultiSelection are for if I understand you correctly.

Cheers

Sebastian
|
On 01/23/2016 01:07 AM, Frank Rueter | OHUfx wrote:
> 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)
>
>
> I also tried adding this to no avail:
>          super(FileChoserView, self).selectionChanged(selected, deselected)
>
>
> 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>* *
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160123/9ecc4c95/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 2666 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160123/9ecc4c95/attachment.png>


More information about the PySide mailing list