Al final preferí usar esta otra clase con las correciones que aparecen en el código y ya funciona.
customitemdelegate.h.-
@
#ifndef CUSTOMITEMDELEGATE_H
#define CUSTOMITEMDELEGATE_H
#include <QItemDelegate>
class CustomItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit CustomItemDelegate(QObject *parent = 0);
virtual void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const ;
virtual QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const ;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
signals:
void updButtonClicked(const QModelIndex &index);
void delButtonClicked(const QModelIndex &index);
public slots:
private:
QStyle::State _state;
QRect oldRect;
};
#endif // CUSTOMITEMDELEGATE_H
@
customitemdelegate.cpp.-
@
#include "customitemdelegate.h"
#include <QModelIndex>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QPainter>
#include <QStyleOptionButton>
#include <QApplication>
#include <QEvent>
#include <QDebug>
#include <QMouseEvent>
CustomItemDelegate::CustomItemDelegate(QObject *parent) :
QItemDelegate(parent)
{
_state = QStyle::State_Enabled;
}
void CustomItemDelegate::paint(QPainter painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
//const QStandardItemModel model = static_cast<const QStandardItemModel*>(index.model());
//QStandardItem* item = model->item(index.row());
//QString text = item->text();
QString text = index.data().toString();
QRect rect = option.rect;
//QRect textRect(rect);
//textRect.setHeight(30);
//painter->drawText(textRect,text);
painter->drawText(rect,text);
QRect buttonRect(rect);
//buttonRect.setY(textRect.y() + 35);
//buttonRect.setY(rect.y() + 35);
buttonRect.setHeight(30);
QStyleOptionButton button;
button.rect = buttonRect;
button.text = text;
button.state = _state | QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
}
QSize CustomItemDelegate::sizeHint(const QStyleOptionViewItem &/option/,
const QModelIndex &/index/) const
{
return QSize(800,70);
}
bool CustomItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
qDebug() << "editorEvent on row: " << index.row();
qDebug() << "editorEvent:: " << event->type();
if( event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease ) {
} else {
_state = QStyle::State_Raised;
return true;
}
QRect buttonRect(option.rect);
//buttonRect.setY(option.rect.y() + 35);
buttonRect.setHeight(30);
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if( !buttonRect.contains( mouseEvent->pos()) ) {
_state = QStyle::State_Raised;
return true;
}
if( event->type() == QEvent::MouseButtonPress) {
_state = QStyle::State_Sunken;
} else if(event->type() == QEvent::MouseButtonRelease) {
_state = QStyle::State_Raised;
if(index.data().toString().contains("Update"))
emit updButtonClicked(index);
else if(index.data().toString().contains("Delete"))
emit delButtonClicked(index);
else
qDebug() << "¡¡¡ --ERROR-- !!!" << index.row();
}
return true;
}
@
No duplicaba el ultimo botón sino que los había desplazado en la columna hacia abajo.
Gracias,
oggie