[Interest] Item editor at preferred size when using custom delegate?
Elvis Stansvik
elvstone at gmail.com
Fri Feb 12 15:03:35 CET 2016
2016-02-12 14:59 GMT+01:00 Elvis Stansvik <elvstone at gmail.com>:
> Hi all,
>
> I have a very basic item delegate which opens up a custom editor:
>
> class MineralLevelsDelegate(QStyledItemDelegate):
>
> def createEditor(self, parent, option, index):
> return MineralLevelsEditor(parent)
>
> def updateEditorGeometry(self, editor, option, index):
> editor.setGeometry(
> option.rect.x(), option.rect.y() + option.rect.height(),
> editor.sizeHint().width(), editor.sizeHint().height())
>
For posterity, this is the custom editor widget:
class MineralLevelsEditor(QFrame):
def __init__(self, parent=None):
super(MineralLevelsEditor, self).__init__(parent)
self._levels = None
self.setAutoFillBackground(True)
self.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
self.setLayout(QFormLayout())
@pyqtProperty(QVariant, user=True)
def levels(self):
return self._levels
@levels.setter
def levels(self, levels):
self._levels = levels
for mineral, level in levels.items():
slider = QSlider(Qt.Horizontal)
slider.setMinimum(0)
slider.setMaximum(100)
slider.setValue(level)
slider.valueChanged.connect(partial(self._setLevel, mineral))
self.layout().addRow(mineral + ':', slider)
def _setLevel(self, mineral, level):
self._levels[mineral] = level
Elvis
> What you see in updateEditorGeometry(...) above is my attempt at
> making the editor show up at its preferred size (instead of being
> constrained to the cell in the QTreeView I'm using). My attempt is to
> use the editor's sizeHint() in the call to setGeometry(...).
>
> The problem seems to be that the sizeHint() or the editor is 0x0 at
> that point (has not been laid out?), and consequently the editor show
> up as a tiny little rect (see attached sizehint.png). I'm also
> attaching hardcoded.png, which shows the editor when I instead
> hardcode it to 200x100. This is kind of the look I want, but obviously
> I don't want to hardcode it to 200x100, but have it use its preferred
> size.
>
> So question is: How can I make the editor widget show up at its preferred size?
>
> Many thanks in advance.
>
> Elvis
More information about the Interest
mailing list