Hi to all,
I'm having a problem with selection of an delegated item which represents two text lines and a CE_PushButton. I want to have the row selected by clicking the button too. Here is my paint function code:
void ColumnTwoLinesDelegate::paint(
) const
{
if (!index.isValid())
return;
if (option.
state & QStyle::State_Selected) {
painter
->setPen
(QPen(Qt
::white));
if (option.
state & QStyle::State_Active) {
}
else
{
}
painter->drawRect(option.rect);
}
else
{
painter
->setPen
(QPen(Qt
::black));
}
// ...it draws two text lines.
painter->restore();
tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
tButtonRect.setWidth(kButtonSize);
tButtonRect.setHeight(kButtonSize);
if (index.model()->data(index.model()->index(index.row(), 5)).toInt() == 1)
{
tButton.icon = mArchive;
}
else
{
tButton.icon = mReActive;
}
tButton.
iconSize = QSize(kButtonSize
- 2, kButtonSize
- 2);
tButton.rect = tButtonRect;
if (mCurrentRow == index.row())
{
tButton.
state = QStyle::State_Sunken |
QStyle::State_Enabled;
}
else
{
tButton.
state = QStyle::State_Raised |
QStyle::State_Enabled;
}
}
void ColumnTwoLinesDelegate::paint(
QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index
) const
{
if (!index.isValid())
return;
if (option.state & QStyle::State_Selected)
{
painter->setPen(QPen(Qt::white));
if (option.state & QStyle::State_Active)
{
painter->setBrush(QBrush(QPalette().highlight()));
}
else
{
painter->setBrush(QBrush(QPalette().color(QPalette::Inactive, QPalette::Highlight)));
}
painter->drawRect(option.rect);
}
else
{
painter->setPen(QPen(Qt::black));
}
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
// ...it draws two text lines.
painter->restore();
QRect tButtonRect;
tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
tButtonRect.setWidth(kButtonSize);
tButtonRect.setHeight(kButtonSize);
QStyleOptionButton tButton;
if (index.model()->data(index.model()->index(index.row(), 5)).toInt() == 1)
{
tButton.icon = mArchive;
}
else
{
tButton.icon = mReActive;
}
tButton.iconSize = QSize(kButtonSize - 2, kButtonSize - 2);
tButton.rect = tButtonRect;
tButton.features |= QStyleOptionButton::Flat;
if (mCurrentRow == index.row())
{
tButton.state = QStyle::State_Sunken | QStyle::State_Enabled;
}
else
{
tButton.state = QStyle::State_Raised | QStyle::State_Enabled;
}
QApplication::style()->drawControl(QStyle::CE_PushButton, &tButton, painter);
}
To copy to clipboard, switch view to plain text mode
and the editorEvent function code:
bool ColumnTwoLinesDelegate::editorEvent(
{
Q_UNUSED(model);
if (event
->type
() != QEvent::MouseButtonPress && event
->type
() != QEvent::MouseButtonRelease) {
return true;
}
// this rect represents a pushbutton
tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
tButtonRect.setWidth(kButtonSize);
tButtonRect.setHeight(kButtonSize);
QMouseEvent* mouseEvent
= static_cast<QMouseEvent
*>
(event
);
// if mouse position is outside button rect/button area we use a State_Raised style
if (!tButtonRect.contains(mouseEvent->pos()))
{
// it allows the row's selection
return false;
}
if (event
->type
() == QEvent::MouseButtonPress) {
mCurrentRow = index.row();
}
else if (event
->type
() == QEvent::MouseButtonRelease) {
mCurrentRow = -1;
// when MouseButtonRelease emit a signal like clicked()
emit documentRequest(index);
}
return true;
}
bool ColumnTwoLinesDelegate::editorEvent(
QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
Q_UNUSED(model);
if (event->type() != QEvent::MouseButtonPress
&& event->type() != QEvent::MouseButtonRelease)
{
return true;
}
// this rect represents a pushbutton
QRect tButtonRect;
tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
tButtonRect.setWidth(kButtonSize);
tButtonRect.setHeight(kButtonSize);
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
// if mouse position is outside button rect/button area we use a State_Raised style
if (!tButtonRect.contains(mouseEvent->pos()))
{
// it allows the row's selection
return false;
}
if (event->type() == QEvent::MouseButtonPress)
{
mCurrentRow = index.row();
}
else if (event->type() == QEvent::MouseButtonRelease)
{
mCurrentRow = -1;
// when MouseButtonRelease emit a signal like clicked()
emit documentRequest(index);
}
return true;
}
To copy to clipboard, switch view to plain text mode
only returning false allows the row selection, but I'd like to obtain the same behavior by clicking the button, is it possible?
Thanks in advance, Danilo
Bookmarks