HI, I have the following strange behaviour: I created a QListView whose elements are delegates. The delegate contains a button implemented this way:
void ItemDelegate
::paint ( QPainter* painter,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
...
button.rect = buttonRect;
button.
state = _state |
QStyle::State_Enabled;
...
}
{
if( event
->type
() == QEvent::MouseButtonPress ||
event
->type
() == QEvent::MouseButtonRelease ) { } else {
_state
= QStyle::State_Raised;
return true;
}
...
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;
// call slot
}
}
void ItemDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
...
QStyleOptionButton button;
button.rect = buttonRect;
button.features |= QStyleOptionButton::Flat;
button.state = _state | QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
...
}
bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if( event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease ) {
} else {
_state = QStyle::State_Raised;
return true;
}
...
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;
// call slot
}
}
To copy to clipboard, switch view to plain text mode
It works fine except that the first time (after starting the application) I click any button, all buttons show the State_Sunken; all others clicks, after the first one, are normals: only the clicked button show the sunken/raised effect.
Do you have any explaination for this?
Very thanks.
Bookmarks