Hi,
I have subclass from QItemDelegate to have a button in tableView Just like this:
qbn7l.png
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,
{
Q_UNUSED(index);
// getting the rect of the cell
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;
&button,painter);
painter
->drawText
(QRect(r.
left() + 5,r.
top(),r.
width() - w,r.
height()),
(Qt::AlignVCenter | Qt::AlignLeft),
index.data(Qt::DisplayRole).toString());
}
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());
}
To copy to clipboard, switch view to plain text mode
Editor Event:
bool TButtonDelegate
::editorEvent(QEvent *event,
{
if( event
->type
() == QEvent::MouseButtonRelease ) {
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;
}
}
}
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,index);
}
To copy to clipboard, switch view to plain text mode
Bookmarks