PDA

View Full Version : QStyledItemDelegate with custom QWidget editor



stefanadelbert
14th July 2010, 09:50
I've created a custom QStyledItemDelegate which uses a custom QWidget for an editor.

When the editor widget is created it's not being painted in the cell that's being edited. It's painted as a separate dialog window.

The editor should contain a QSpinBox and a QToolButton.

Here are my delegate and editor classes:

Delegate



class CQuantityItemDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
CQuantityItemDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}

void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;

private slots:
void commitAndCloseEditor();
};

void
CQuantityItemDelegate::paint(
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyledItemDelegate::paint(painter, option, index);
}

QSize
CQuantityItemDelegate::sizeHint(
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
return QStyledItemDelegate::sizeHint(option, index);
}

QWidget*
CQuantityItemDelegate::createEditor(
QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
CQuantityEditor* editor = new CQuantityEditor(parent);
connect(editor, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
return editor;
}

void
CQuantityItemDelegate::setEditorData(
QWidget* editor,
const QModelIndex& index) const
{
int value = index.model()->data(index, Qt::EditRole).toInt();
CQuantityEditor* quantityEditor = static_cast<CQuantityEditor*>(editor);
quantityEditor->SetQuantity(value);
}

void
CQuantityItemDelegate::setModelData(
QWidget* editor,
QAbstractItemModel* model,
const QModelIndex& index) const
{
QStyledItemDelegate::setModelData(editor, model, index);
}

void
CQuantityItemDelegate::commitAndCloseEditor()
{
CQuantityEditor* editor = qobject_cast<CQuantityEditor*>(sender());
emit commitData(editor);
emit closeEditor(editor);
}


Editor



class CQuantityEditor : public QWidget
{
Q_OBJECT

public:
CQuantityEditor(QWidget *parent = 0) {
quantitySpinBox = new QSpinBox(this);
// confirm = new QToolButton(parent);
// confirm->setIcon(QIcon(":/Icons/Box/Tick/green"));
// confirm->setToolButtonStyle(Qt::ToolButtonIconOnly);
//
// QHBoxLayout* hLayout = new QHBoxLayout(parent);
// hLayout->addWidget(quantitySpinBox, 1);
// hLayout->addWidget(confirm, 0);
// hLayout->setSpacing(0);
// hLayout->setMargin(0);
// setLayout(hLayout);
//
// connect(confirm, SIGNAL(released()),
// this, SIGNAL(editingFinished())
// );
}

void SetQuantity(const int& quantity) {
quantitySpinBox->setValue(quantity);
}
int Quantity() { return quantitySpinBox->value(); }

signals:
void editingFinished();

protected:
void paintEvent(QPaintEvent* e) {
QWidget::paintEvent(e);
}

private:
QSpinBox* quantitySpinBox;
QToolButton* confirm;
};

wysota
14th July 2010, 10:36
Please provide a minimal compilable example reproducing the problem.

agathiyaa
14th July 2010, 15:30
CQuantityEditor(QWidget *parent = 0):QWidget(parent) ----> look at your constructor ~!

stefanadelbert
14th July 2010, 23:19
Yes, indeed. That's what I was missing. How could I have missed that?!?

@agathiyaa Thanks very much!