Rather perplexed by this omission--but in Qt's QAbstractItemView class, it's possible to set a QAbstractItemDelegate (i.e., QItemDelegate or QStyledItemDelegate) to the entire view, a single row, or a single column, using the setItemDelegate*
methods. In addition the item delegate for an individual cell can be queried, with QAbstractItemView::itemDelegate(const QModelIndex&)
, along with the delegate for rows, columns. and the entire view. But there appears to be no way to set an item delegate to an individual cell. Am I missing something? Any reason this should be?
How to set a delegate for a single cell in Qt item view?
Asked Answered
No you can't set item delegate only for one cell or one column but you can easly set item delegate for whole widget and choose in which cell, column or row you want to use your custom painting or something.
For e.g.
void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 1)
{
// ohh it's my column
// better do something creative
}
else // it's just a common column. Live it in default way
QItemDelegate::paint(painter, option, index);
}
You can find some more information here
This is my first time using delegates--on your method, if the delegate I set is a spinbox, would editing only reveal the spinbox for column 1? Would the spinbox range not be in effect for the other columns? –
Evolve
Every things will affect only column number 1. All other columns will saty as they were earlier. –
Justis
Alright, but on this method is it possible to have more than one cell-specific delegate type? Could you have a spinbox at (0,2) and a progress bar at (3,1) with default delegates everywhere else? –
Evolve
Yes, all that you can do by one ItemDelegate. Add else if after this one if and check for proper column and row –
Justis
I'm trying to implement what you wrote now and I find I have no idea how to specify within the
if (index...
block what the widget for that particular case should be--can you fill that in with example code? –
Evolve I paste link for you in my answer. There is all written. –
Justis
-1 'No you can't set item delegate only for one cell or one column' Yes you can, check
void QAbstractItemView::setItemDelegateForColumn ( int column, QAbstractItemDelegate * delegate )
–
Noella I'd recommend reimplementing createEditor function instead:
QWidget * WidgetDelegate::createEditor(
QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex &index) const
{
QWidget *widget = 0;
if (index.isValid() && index.column() < factories.size())
{
widget = factories[index.column()]->createEditor(index.data(Qt::EditRole).userType(), parent);
if (widget)
widget->setFocusPolicy(Qt::WheelFocus);
}
return widget;
}
Could you demonstrate this being used with a comboBox? –
Sexennial
Sinc Qt 4.2, QAbstractItemView provides setItemDelegateForColumn() and setItemDelegateForRow().
© 2022 - 2024 — McMap. All rights reserved.