PDA

View Full Version : Button Delegate Problem in TableView



alizadeh91
17th March 2013, 08:41
Hi,

I have subclass from QItemDelegate to have a button in tableView Just like this:

8828

I've done this :) But there is problem with mouse hover in this button.
Other buttons as you know get focused and highlighted when mouse is hovered
But in this case nothing happens when mouse enters the button. How can i do this??
I think somethings have to added into editorEvent method, but in this method i haven't
access to those buttons which have painted in paint method...
Any suggestion?

Paint Method:


void TButtonDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(index);

QStyleOptionButton button;

// getting the rect of the cell
QRect r = option.rect;

int x,y,w,h;

// The x coordinate of the cell
x = r.left() + r.width() - 30;

y = r.top();

w = 30;

h = r.height();

button.rect = QRect(x,y,w,h);

button.text = "<=";

button.state = QStyle::State_Enabled;

QApplication::style()->drawControl(QStyle::CE_PushButton,
&button,painter);

painter->drawText(QRect(r.left() + 5,r.top(),r.width() - w,r.height()),
(Qt::AlignVCenter | Qt::AlignLeft),
index.data(Qt::DisplayRole).toString());
}


Editor Event:




bool TButtonDelegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent * e = (QMouseEvent *)event;

int clickX = e->x();

int clickY = e->y();

QRect r = option.rect;//getting the rect of the cell

int x,y,w,h;

x = r.left() + r.width() - 30;//the X coordinate

y = r.top();//the Y coordinate

w = 30;//button width

h = 30;//button height

if( clickX > x && clickX < x + w )
if( clickY > y && clickY < y + h )
{
emit cellButtonClicked(index);
return true;
}
}

return QItemDelegate::editorEvent(event,model,option,inde x);
}

alizadeh91
17th March 2013, 12:59
I think i have to change state of button, but how can i do that when mouse is over the button? !

Added after 1 30 minutes:

I've Solved. :) But in this way can the button get stylesheet?


void TButtonDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(index);

QStyleOptionButton button;

// getting the rect of the cell
QRect r = option.rect;

int x,y,w,h;

// The x coordinate of the cell
x = r.left() + r.width() - 30;

y = r.top();

w = 30;

h = r.height();

button.rect = QRect(x,y,w,h);

button.text = "<=";

button.state = option.state;

QApplication::style()->drawControl(QStyle::CE_PushButton,
&button,painter);

painter->drawText(QRect(r.left() + 5,r.top(),r.width() - w,r.height()),
(Qt::AlignVCenter | Qt::AlignLeft),
index.data(Qt::DisplayRole).toString());
}

wysota
17th March 2013, 18:01
There is no button here. You only paint something that looks like a button. Therefore you need to do everything manually. As for the stylesheet, probably the only way to make it work is to set the stylesheet on the application object.

alizadeh91
17th March 2013, 18:13
Thanks wysota :) Yeah I know that ;) I just wanted to know if is there any way.