Sven
13th February 2011, 20:42
Hello folks,
I've got a little issue with my custom StyledItemDelegate. I have got a list view where I want to add a remove button on the first column. This works pretty good but it is just visible in edit mode, but I want to have it visible when I hover the row.
Can anyone give me a hint how to implement this?
buttondelegate.h
#ifndef BUTTONDELEGATE_H_
#define BUTTONDELEGATE_H_
#include <QStyledItemDelegate>
class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ButtonDelegate(QObject *aParent);
QWidget *createEditor(
QWidget *aParent,
const QStyleOptionViewItem &aOption,
const QModelIndex &aIndex) const;
virtual void setEditorData(
QWidget *aEditor,
const QModelIndex &aIndex) const;
virtual void setModelData(
QWidget *aEditor,
QAbstractItemModel *aModel,
const QModelIndex &aIndex) const;
virtual void updateEditorGeometry(
QWidget *aEditor,
const QStyleOptionViewItem &aOption,
const QModelIndex &aIndex) const;
};
#endif
#include "buttondelegate.h"
#include <QToolButton>
#include <QApplication>
//-----------------------------------------------------------------------------
ButtonDelegate::ButtonDelegate(QObject *aParent)
: QStyledItemDelegate(aParent)
{
}
//-----------------------------------------------------------------------------
QWidget *ButtonDelegate::createEditor(
QWidget *aParent,
const QStyleOptionViewItem & /*aOption*/,
const QModelIndex &aIndex) const
{
if (aIndex.column() == 0) {
QToolButton *btn = new QToolButton(aParent);
btn->setText("...");
//btn->setAutoRaise(true);
btn->setCheckable(true);
btn->setFixedSize(QSize(16, 16));
return btn;
}
return NULL;
}
//-----------------------------------------------------------------------------
void ButtonDelegate::setEditorData(
QWidget * /*aEditor */,
const QModelIndex & /*aIndex*/) const
{
}
//-----------------------------------------------------------------------------
void ButtonDelegate::setModelData(
QWidget *aEditor,
QAbstractItemModel *aModel,
const QModelIndex &aIndex) const
{
//QApplication::processEvents();
if (static_cast<QToolButton*>(aEditor)->isChecked())
aModel->removeRow(aIndex.row());
}
//-----------------------------------------------------------------------------
void ButtonDelegate::updateEditorGeometry(
QWidget *aEditor,
const QStyleOptionViewItem &aOption,
const QModelIndex & /*aIndex*/) const
{
aEditor->setGeometry(aOption.rect);
}
And yes I have searched on the forum and found some things but they didn't help.
Kind regards,
Sven
I've got a little issue with my custom StyledItemDelegate. I have got a list view where I want to add a remove button on the first column. This works pretty good but it is just visible in edit mode, but I want to have it visible when I hover the row.
Can anyone give me a hint how to implement this?
buttondelegate.h
#ifndef BUTTONDELEGATE_H_
#define BUTTONDELEGATE_H_
#include <QStyledItemDelegate>
class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ButtonDelegate(QObject *aParent);
QWidget *createEditor(
QWidget *aParent,
const QStyleOptionViewItem &aOption,
const QModelIndex &aIndex) const;
virtual void setEditorData(
QWidget *aEditor,
const QModelIndex &aIndex) const;
virtual void setModelData(
QWidget *aEditor,
QAbstractItemModel *aModel,
const QModelIndex &aIndex) const;
virtual void updateEditorGeometry(
QWidget *aEditor,
const QStyleOptionViewItem &aOption,
const QModelIndex &aIndex) const;
};
#endif
#include "buttondelegate.h"
#include <QToolButton>
#include <QApplication>
//-----------------------------------------------------------------------------
ButtonDelegate::ButtonDelegate(QObject *aParent)
: QStyledItemDelegate(aParent)
{
}
//-----------------------------------------------------------------------------
QWidget *ButtonDelegate::createEditor(
QWidget *aParent,
const QStyleOptionViewItem & /*aOption*/,
const QModelIndex &aIndex) const
{
if (aIndex.column() == 0) {
QToolButton *btn = new QToolButton(aParent);
btn->setText("...");
//btn->setAutoRaise(true);
btn->setCheckable(true);
btn->setFixedSize(QSize(16, 16));
return btn;
}
return NULL;
}
//-----------------------------------------------------------------------------
void ButtonDelegate::setEditorData(
QWidget * /*aEditor */,
const QModelIndex & /*aIndex*/) const
{
}
//-----------------------------------------------------------------------------
void ButtonDelegate::setModelData(
QWidget *aEditor,
QAbstractItemModel *aModel,
const QModelIndex &aIndex) const
{
//QApplication::processEvents();
if (static_cast<QToolButton*>(aEditor)->isChecked())
aModel->removeRow(aIndex.row());
}
//-----------------------------------------------------------------------------
void ButtonDelegate::updateEditorGeometry(
QWidget *aEditor,
const QStyleOptionViewItem &aOption,
const QModelIndex & /*aIndex*/) const
{
aEditor->setGeometry(aOption.rect);
}
And yes I have searched on the forum and found some things but they didn't help.
Kind regards,
Sven