1

I was able to add the QSpinBox widget in a QTreeView using QStyledItemDelegate.

QWidget *NoteDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.column() == 2)
    {
        QSpinBox *spinBox = new QSpinBox(parent);
        spinBox->setRange(0, 9999);

        return spinBox;
    }

    return QStyledItemDelegate::createEditor(parent, option, index);
}

void NoteDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (index.column() == 2)
    {
        int value = index.model()->data(index, Qt::EditRole).toInt();

        auto spinBox = static_cast<QSpinBox *>(editor);
        spinBox->setValue(value);
        return;
    }

    QStyledItemDelegate::setEditorData(editor, index);
}

void NoteDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (index.column() == 2)
    {
        auto spinBox = static_cast<QSpinBox *>(editor);
        int value = spinBox->value();
        model->setData(index, value, Qt::EditRole);
        return;
    }

    QStyledItemDelegate::setModelData(editor, model, index);
}

But, QSpinBox is showing up only when it is in the edit mode. How can I display this QSpinBox all the time even when it is in the display mode?

Jude
  • 11
  • 1

1 Answers1

0

I suspect that QAbstractItemView::openPersistentEditor may be what you are looking for.

Morten B.
  • 93
  • 8