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:
virtual void setEditorData(
virtual void setModelData(
virtual void updateEditorGeometry(
};
#endif
#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
To copy to clipboard, switch view to plain text mode
#include "buttondelegate.h"
#include <QToolButton>
#include <QApplication>
//-----------------------------------------------------------------------------
ButtonDelegate
::ButtonDelegate(QObject *aParent
) : QStyledItemDelegate(aParent)
{
}
//-----------------------------------------------------------------------------
QWidget *ButtonDelegate
::createEditor( {
if (aIndex.column() == 0) {
btn->setText("...");
//btn->setAutoRaise(true);
btn->setCheckable(true);
btn
->setFixedSize
(QSize(16,
16));
return btn;
}
return NULL;
}
//-----------------------------------------------------------------------------
void ButtonDelegate::setEditorData(
{
}
//-----------------------------------------------------------------------------
void ButtonDelegate::setModelData(
{
//QApplication::processEvents();
if (static_cast<QToolButton*>(aEditor)->isChecked())
aModel->removeRow(aIndex.row());
}
//-----------------------------------------------------------------------------
void ButtonDelegate::updateEditorGeometry(
{
aEditor->setGeometry(aOption.rect);
}
#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);
}
To copy to clipboard, switch view to plain text mode
And yes I have searched on the forum and found some things but they didn't help.
Kind regards,
Sven
Bookmarks