[PyQt] custom editor with setFocusProxy set on a QLineEdit child
element won't close
Victor Noagbodji
noagbodjivictor at gmail.com
Wed Jul 29 22:57:20 BST 2009
hi,
i'm having a small issue with a a custom editor, that has a QLineEdit
child, with a setFocusProxy set. now this editor does not behave the
same way would a direct QLineEdit used as an editor.
below is a sample code that should display the behavior i'm talking
about. it's a matter of setting editor to MySpinBox1 or
MySpinBoxEditor.
i'm wondering what are the internal changes done by setFocusProxy. the
documentation doesn't say much about it.
thanks a lot in advance
import sys
from PyQt4 import QtCore, QtGui
class MyHBoxLayout(QtGui.QHBoxLayout):
def __init__(self, parent=None):
QtGui.QHBoxLayout.__init__(self, parent)
self.setContentsMargins(0, 0, 0, 0)
class MySpinBox1(QtGui.QSpinBox):
def __init__(self, parent=None):
QtGui.QSpinBox.__init__(self, parent)
self.setMinimum(0)
self.setMaximum(100)
class MySpinBox2(QtGui.QSpinBox):
def __init__(self, parent=None):
QtGui.QSpinBox.__init__(self, parent)
self.setMinimum(0)
self.setMaximum(100)
policy = (QtGui.QSizePolicy.MinimumExpanding,
QtGui.QSizePolicy.MinimumExpanding)
self.setSizePolicy(QtGui.QSizePolicy(*policy))
class MySpinBoxEditor(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.spinbox = MySpinBox2()
hbox = MyHBoxLayout()
hbox.addWidget(self.spinbox)
self.setLayout(hbox)
self.setFocusProxy(self.spinbox)
def interpretText(self):
self.spinbox.interpretText()
def value(self):
return self.spinbox.value()
def setValue(self, value):
self.spinbox.setValue(value)
class MyDelegate(QtGui.QItemDelegate):
def createEditor(self, parent, option, index):
# editor can be set to MySpinBox1(parent)
editor = MySpinBoxEditor(parent)
editor.installEventFilter(self)
return editor
def setEditorData(self, editor, index):
value, ok = index.model().data(index, QtCore.Qt.DisplayRole).toInt()
editor.setValue(value)
def setModelData(self, editor, model, index):
editor.interpretText()
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 = MyDelegate()
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.show()
sys.exit(app.exec_())
--
paul victor noagbodji
More information about the PyQt
mailing list