How to center an item editor created by a custom item delegate?
As the title says... i have a qitemdelgate which creates a QCheckBox as editor for some items. how can i center this editor in the cell of the qtableview where the editor shows up.
here`s the method i wrote so far
Code:
{
if (index.column() == m_VisibilityColumnIndex)
{
//option.decorationAlignment = Qt::AlignCenter;
//option.decorationAlignment = Qt::AlignCenter;
connect(visibilityCheckBox, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
return visibilityCheckBox;
}
else
{
}
}
Re: How to center an item editor created by a custom item delegate?
Have a look at QStyledItemDelegate::updateEditorGeometry or QItemDelegate::updateEditorGeometry based on what u are using.
You can place your check box based on the rect u get in option.rect :)
Re: How to center an item editor created by a custom item delegate?
i tried to change updateEditorGeometry().
Code:
{
QRect rect
= option.
rect;
if (index.column() == m_VisibilityColumnIndex)
{
rect.setX(rect.x() + 10);
}
editor->setGeometry(rect);
}
but obviously the method never gets called (i set a breakpoint there). so what can i do?
Re: How to center an item editor created by a custom item delegate?
btw: in my code example i just tried to move the checkbox 10 pixels to the right (instead of centering it)-> just for demonstration
Re: How to center an item editor created by a custom item delegate?
Does ur createEditor function gets called ?
If not check if the item is set as editable or not. updateEditorGeometry must be called if the editor is created...
Re: How to center an item editor created by a custom item delegate?
The only way i found is to set a QMyStyle to the QTableView
m_pMyTableView->setStyle(new QMyStyle());
In QMyStyle you should overload subElementRect(...) Method as follow :
QRect QMyStyle::subElementRect(SubElement sr, const QStyleOption *opt, const QWidget *widget) const
{
//die CheckBox mittig ausrichten
if (sr == QStyle::SE_ItemViewItemCheckIndicator || sr == QStyle::SE_CheckBoxIndicator)
{
QRect r = QWindowsVistaStyle::subElementRect(sr, opt, widget);
int center = opt->rect.center().x();
return QRect(QPoint(center - r.width() / 2, r.y()), QPoint(center + r.width() / 2, r.bottom()));
}
return QWindowsVistaStyle::subElementRect(sr, opt, widget);
}
regards,
Max