Results 1 to 3 of 3

Thread: QItemDelegate's QComboBox default value?

  1. #1
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Question QItemDelegate's QComboBox default value?

    Is there a legitimate means of setting a models value via a delegated QComboBox's default value (within a QtableView)?
    Upon clicking the ComboBox, the model data is properly registered - but prior to this, the data is empty.

    This is because @ the comboBox's creation, the index is effectively '-1' correct?

    I have tried to emplace various calls to:
    QComboBox.setCurrentIndex(0) within the delegate, but because it has yet to return the editor object to the View, the signal for 'currentIndexChanged' is never called, and thus the data model is never updated.

    I suppose I could blunt code editing of the model data from the View class with hard strings for the default values - but I wondered if there was a means of keeping in 'sync' with the comboBox.


    [SIDE NOTE: Is it possible to set a stylesheet within the delegate for the combobox? Currently unable to with 'confinedCombo.setStyleSheet(...)']

    Thanks.

    Qt Code:
    1. class quiteANiceView(QtGui.QDialog, QtGui.QWidget):
    2. def __init__(self, iface, editorType, directory, parent=None):
    3. super(shapeCSVeditor, self).__init__(parent)
    4. self.iface = iface
    5. self.interpol = interpol()
    6. self.editorType = editorType
    7. self.directory = directory
    8. self.tableView = QtGui.QTableView(self)
    9. ...
    10. if 'Layer-Options' in self.editorType:
    11. self.tableView.setItemDelegateForColumn(1,confineComboDelegate(self.tableView))
    12. self.tableView.openPersistentEditor(self.tableData.index(row, 1)
    13. ...
    14.  
    15. class confineComboDelegate(QtGui.QItemDelegate):
    16. def __init__(self, parent):
    17. QtGui.QItemDelegate.__init__(self, parent)
    18. def createEditor(self, parent, option, index):
    19. confiningChoices = ['True','False']
    20. confineCombo = QtGui.QComboBox(parent)
    21. confineCombo.addItems(self.confiningChoices)
    22. confineCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
    23. return confineCombo
    24. def setEditorData(self, editor, index):
    25. editor.blockSignals(True)
    26. try: idxConversion = self.confiningChoices.index(index.model().data(index,Qt.DisplayRole))
    27. except: idxConversion = 0
    28. editor.setCurrentIndex(int(idxConversion))
    29. editor.blockSignals(False)
    30. def setModelData(self, editor, model, index):
    31. model.setData(index, self.confiningChoices[editor.currentIndex()],Qt.EditRole)
    32. @QtCore.pyqtSlot(int)
    33. def currentIndexChanged(self):
    34. self.commitData.emit(self.sender())
    To copy to clipboard, switch view to plain text mode 
    Last edited by jkrienert; 28th January 2015 at 14:30.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QItemDelegate's QComboBox default value?

    If you want to set the opening value in the editor, either from the model's current value or a default value, then you do this in setEditorData() as in your example. After that, there is no connection between the data in the editor and the data in the model until the editor is closed (by leaving the cell etc.) and the setModelData() function is called.

    If your model needs to have a default value in a column then the model should probably do that in insertRows(). Failing that, you can probably call setData() from a slot attached to the rowsInserted() signal of the model or, as a last resort, in setModelData() when the existing data is a null QVariant.

  3. The following user says thank you to ChrisW67 for this useful post:

    jkrienert (28th January 2015)

  4. #3
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Smile Re: QItemDelegate's QComboBox default value?

    then you do this in setEditorData() as in your example.
    I am a bit confused of a small aspect of this. Via my example, the try:/except: block sets the index to zero... and while the initial table view displays in accordance with the index setting, the actual value is not emplaced in the data model - as discovered by simply saving the changes immediately after starting the table view (and checking the output file). IF a cell (comboBox) is clicked, then subsequently the unaltered / altered value is properly registered to the data model and observed in the file Output.
    (hopefully that made sense).

    It seems that another work-around is in order to get the default values to be properly inserted into the dataModel, even if the user does not make an initial selection of the cell (comboBox). My initial actions are warranted based on setModelData not initially being called, so possibly calling it 'manually' upon startup would be a solution? Yet, I am curious as to how the delegate sources the model information - as I get an error with the following (based on the model argument) mainly because I am a bit novice on how to instance the QAbstractTableModel in this circumstance:

    Qt Code:
    1. class averageComboDelegate(QtGui.QItemDelegate):
    2. def __init__(self, parent):
    3. QtGui.QItemDelegate.__init__(self, parent)
    4. def createEditor(self, parent, option, index):
    5. self.averageChoices = ['Arithmetic','Harmonic','Logarithmic']
    6. averageCombo = QtGui.QComboBox(parent)
    7. averageCombo.addItems(self.averageChoices)
    8. averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
    9. return averageCombo
    10. def setEditorData(self, editor, index):
    11. editor.blockSignals(True)
    12. try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
    13. except ValueError: idxConversion = 0
    14. editor.setCurrentIndex(int(idxConversion))
    15. editor.blockSignals(False)
    16. self.setModelData(self,editor,???,index) #<<============================?
    17. def setModelData(self, editor, model, index):
    18. model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
    19. @QtCore.pyqtSlot(int)
    20. def currentIndexChanged(self):
    21. self.commitData.emit(self.sender())
    To copy to clipboard, switch view to plain text mode 


    Added after 5 minutes:

    Also:

    When I turn the block off during 'priming' of the comoboBox - the signal to change data is not called, I suspect because this is occurring during the initialization of the widget? (frankly both these links could be omitted if this worked, but this could possibly create an infinite loop correct?)

    Qt Code:
    1. ...
    2. averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
    3. return averageCombo
    4. def setEditorData(self, editor, index):
    5. editor.blockSignals(False) #<<=======================Was originally 'True'
    6. try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
    7. except ValueError: idxConversion = 0
    8. editor.setCurrentIndex(int(idxConversion))
    9. editor.blockSignals(False)
    10. def setModelData(self, editor, model, index):
    11. model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
    To copy to clipboard, switch view to plain text mode 

    then you do this in setEditorData() as in your example.
    I am a bit confused of a small aspect of this. Via my example, the try:/except: block sets the index to zero... and while the initial table view displays in accordance with the index setting, the actual value is not emplaced in the data model - as discovered by simply saving the changes immediately after starting the table view (and checking the output file). IF a cell (comboBox) is clicked, then subsequently the unaltered / altered value is properly registered to the data model and observed in the file Output.
    (hopefully that made sense).

    It seems that another work-around is in order to get the default values to be properly inserted into the dataModel, even if the user does not make an initial selection of the cell (comboBox). My initial actions are warranted based on setModelData not initially being called, so possibly calling it 'manually' upon startup would be a solution? Yet, I am curious as to how the delegate sources the model information - as I get an error with the following (based on the model argument) mainly because I am a bit novice on how to instance the QAbstractTableModel in this circumstance:

    Qt Code:
    1. class averageComboDelegate(QtGui.QItemDelegate):
    2. def __init__(self, parent):
    3. QtGui.QItemDelegate.__init__(self, parent)
    4. def createEditor(self, parent, option, index):
    5. self.averageChoices = ['Arithmetic','Harmonic','Logarithmic']
    6. averageCombo = QtGui.QComboBox(parent)
    7. averageCombo.addItems(self.averageChoices)
    8. averageCombo.currentIndexChanged[int].connect(self.currentIndexChanged)
    9. return averageCombo
    10. def setEditorData(self, editor, index):
    11. editor.blockSignals(True)
    12. try: idxConversion = self.averageChoices.index(index.model().data(index,Qt.DisplayRole))
    13. except ValueError: idxConversion = 0
    14. editor.setCurrentIndex(int(idxConversion))
    15. editor.blockSignals(False)
    16. self.setModelData(self,editor,???,index) #<<============================?
    17. def setModelData(self, editor, model, index):
    18. model.setData(index, self.averageChoices[editor.currentIndex()],Qt.EditRole)
    19. @QtCore.pyqtSlot(int)
    20. def currentIndexChanged(self):
    21. self.commitData.emit(self.sender())
    To copy to clipboard, switch view to plain text mode 


    Added after 39 minutes:


    'Bleck...'
    The code below is the solution I have settled upon (which isn't to kind on the eyes); and its the only one I have found to work (so far).
    I suppose there might be an alternative, and if its discovered - I will update this posting for the probably miniscule list of people whom might be searching for a similar solution.

    Its merely a function within the QAbstractTableModel which houses the default values for each particular column.
    If the initlized values in the table (from source file) are found to be None-Type, then the defaults are inserted respectively.

    Qt Code:
    1. def setDefaultCombos(self):
    2. if 'Layer-Options' in self.editorType:
    3. for row in range(len(self.rows)):
    4. if self.rows[row][1] == '': \
    5. self.setData(self.index(row,1,QtCore.QModelIndex()),'True',Qt.EditRole)
    6. if self.rows[row][2] == '': \
    7. self.setData(self.index(row,2,QtCore.QModelIndex()),'Confined',Qt.EditRole)
    8. if self.rows[row][3] == '': \
    9. self.setData(self.index(row,3,QtCore.QModelIndex()),'Arithmetic',Qt.EditRole)
    10. if self.rows[row][5] == '': \
    11. self.setData(self.index(row,5,QtCore.QModelIndex()),'Actual Value',Qt.EditRole)
    12. if self.rows[row][6] == '': \
    13. self.setData(self.index(row,6,QtCore.QModelIndex()),'Active',Qt.EditRole)
    14. if 'Stress Period-Options' in self.editorType:
    15. for row in range(len(self.rows)):
    16. if self.rows[row][4] == '': \
    17. self.setData(self.index(row,4,QtCore.QModelIndex()),'Steady State',Qt.EditRole)
    To copy to clipboard, switch view to plain text mode 
    Last edited by jkrienert; 29th January 2015 at 13:08. Reason: formating for readability

Similar Threads

  1. QItemDelegate for painting QComboBox in QTreeWidget
    By naturalpsychic in forum Qt Programming
    Replies: 7
    Last Post: 23rd May 2011, 15:38
  2. QItemDelegate, QDataWidgetMapper and QComboBox
    By cydside in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2009, 18:44
  3. QItemDelegate and default actions...
    By TemporalBeing in forum Qt Programming
    Replies: 1
    Last Post: 4th April 2009, 17:45
  4. Editable QComboBox with QItemDelegate
    By Jmgr in forum Qt Programming
    Replies: 11
    Last Post: 10th December 2008, 09:21
  5. QComboBox QitemDelegate size
    By aekilic in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2008, 16:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.