PDA

View Full Version : qpushbutton delegate



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?

amleto
8th November 2012, 21:57
No. those are your constants... How does Qt know what State is?

but it would have been considerate for you to mention change *relative to what*.

norobro
8th November 2012, 22:34
No. those are your constants...
Exactly as the OP explained in the thread to which we weren't provided a link (http://www.qtcentre.org/threads/26916-inserting-custom-Widget-to-listview?p=128623#post128623).

(State is my own enum)