2 つのテキスト行と CE_PushButton を表す委任アイテムの選択に問題があります。ボタンをクリックして行を選択したい。これが私のペイント関数コードです:
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);
}
およびeditorEvent関数コード:
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;
}
falseを返すだけで行を選択できますが、ボタンをクリックして同じ動作を得たいと思います。出来ますか?前もって感謝します、ダニロ