giugio
8th November 2012, 21:18
hello .
I wish create a button delegate(with a qpushbutton widget) that is drawed "On" normaly ,and drawed pushed when is clicked.
i found this on this forum :
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
if (!index.isValid() || index.column() != ButtonColumn) {
return;
}
QStyleOptionButton opt;
State s = (State)(index.data(Qt::UserRole).toInt());
if (s == Hovered)
opt.state |= QStyle::State_MouseOver;
if (s == Pressed)
opt.state |= QStyle::State_Sunken;
opt.state |= QStyle::State_Enabled;
opt.rect = option.rect.adjusted(1, 1, -1, -1);
opt.text = trUtf8("Button text");
QApplication::style()->drawControl(QStyle::CE_PushButton, &opt, painter, 0);
}
the question is on point :
State s = (State)(index.data(Qt::UserRole).toInt());
and
if (s == Hovered)
or
if (s == Pressed)
i use qt 4.8 may be that some constants are changed?
I wish create a button delegate(with a qpushbutton widget) that is drawed "On" normaly ,and drawed pushed when is clicked.
i found this on this forum :
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
if (!index.isValid() || index.column() != ButtonColumn) {
return;
}
QStyleOptionButton opt;
State s = (State)(index.data(Qt::UserRole).toInt());
if (s == Hovered)
opt.state |= QStyle::State_MouseOver;
if (s == Pressed)
opt.state |= QStyle::State_Sunken;
opt.state |= QStyle::State_Enabled;
opt.rect = option.rect.adjusted(1, 1, -1, -1);
opt.text = trUtf8("Button text");
QApplication::style()->drawControl(QStyle::CE_PushButton, &opt, painter, 0);
}
the question is on point :
State s = (State)(index.data(Qt::UserRole).toInt());
and
if (s == Hovered)
or
if (s == Pressed)
i use qt 4.8 may be that some constants are changed?