PDA

View Full Version : How to center an item editor created by a custom item delegate?



bar0n
4th March 2009, 12:19
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



QWidget* QmitkDataStorageDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option
, const QModelIndex &index) const
{
if (index.column() == m_VisibilityColumnIndex)
{
//option.decorationAlignment = Qt::AlignCenter;
//option.decorationAlignment = Qt::AlignCenter;

QCheckBox* visibilityCheckBox = new QCheckBox(parent);
connect(visibilityCheckBox, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
return visibilityCheckBox;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}

aamer4yu
4th March 2009, 17:12
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 :)

bar0n
6th March 2009, 12:45
i tried to change updateEditorGeometry().


void MyDelegate::updateEditorGeometry( QWidget * editor, const QStyleOptionViewItem option, const QModelIndex & index ) const
{
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?

bar0n
6th March 2009, 12:46
btw: in my code example i just tried to move the checkbox 10 pixels to the right (instead of centering it)-> just for demonstration

aamer4yu
6th March 2009, 15:01
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...

maxwalter
10th August 2009, 10:50
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