[PyQt] problem with createEditor method of QItemDelegate

TP paratribulations at free.fr
Wed Aug 12 17:11:43 BST 2009


Hi,

Try the example at the end of this message: it is a table with editable
items. This is a modified version of the example "spinboxdelegate.py" of
the PyQt examples ("itemviews" section). When the user double-clicks on an
item, a LabelAndSpinBox instance is created as delegate. The problem is
that the QHBoxLayout layout of this delegate does not expand to occupy all
the available space; therefore the item data value remains visible and the
delegates is too small. How to make it work correctly?

Thanks,

Julien

PS1: this is a simple example, but in a more elaborated version, the
delegate does not appear at all when the user double clicks. It is
displayed correctly if no parent is given (it appears in a separate
window), but when defining a parent (the "QWidget * parent" given to
createEditor) as parent of the delegate, it does not appear at all.
Perhaps solving the size problem on the simple example below will solve the
problem in this more elaborated version.

PS2:
>>> qVersion()
'4.4.0'
>>> PYQT_VERSION_STR
'4.4.4'


#######################################
#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui


class LabelAndSpinBox( QtGui.QWidget ):

    def __init__( self, parent ):

        super( LabelAndSpinBox, self ).__init__( parent )

        qh = QtGui.QHBoxLayout( self )
        self.qlabel = QtGui.QLabel()
        self.qlabel.setText( "foobar" )
        qh.addWidget( self.qlabel )

        self.qspinbox = QtGui.QSpinBox()
        self.qspinbox.setValue( 0 )
        qh.addWidget( self.qspinbox )

        self.toolbutton = QtGui.QToolButton()
        qh.addWidget( self.toolbutton )

        self.setLayout( qh )


    def setMinimum( self, value ):

        self.qspinbox.setMinimum( value )


    def setMaximum( self, value ):

        self.qspinbox.setMaximum( value )


    def setValue( self, value ):

        self.qspinbox.setValue( value )


    def value( self ):

        return self.qspinbox.value()





class SpinBoxDelegate(QtGui.QItemDelegate):

    def __init__(self, parent = None):

        QtGui.QItemDelegate.__init__(self, parent)

    def createEditor(self, parent, option, index):

        editor = LabelAndSpinBox( parent )
        editor.setMinimum( 0 )
        editor.setMaximum( 100 )
        # editor.installEventFilter(self)
        return editor

    def setEditorData( self, spinBox, index ):

        value, ok = index.model().data(index, QtCore.Qt.DisplayRole).toInt()
        spinBox.setValue(value)

    def setModelData(self, editor, model, index):

        value = editor.value()
        model.setData( index
                , QtCore.QVariant( value ) )

    def updateEditorGeometry( self, editor, option, index ):

        editor.setGeometry(option.rect)


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)

    model = QtGui.QStandardItemModel(4, 2)
    tableView = QtGui.QTableView()
    tableView.setModel(model)

    delegate = SpinBoxDelegate()
    tableView.setItemDelegate(delegate)

    for row in range(4):
        for column in range(2):
            index = model.index(row, column, QtCore.QModelIndex())
            model.setData(index, QtCore.QVariant((row+1) * (column+1)))

    tableView.setWindowTitle("Spin Box Delegate")
    tableView.show()
    sys.exit(app.exec_())
#######################################

-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)



More information about the PyQt mailing list