void MyDelegate
::paint(QPainter *painter,
{
Q_ASSERT(model);
if (qVariantCanConvert<MyCustomsClass>(index.data(MyModel::DeviceInfoRole)))
{
MyCustomsClass info = qVariantValue<MyCustomsClass>(index.data(MyModel::DeviceInfoRole));
buttonOption.state = option.state;
#ifdef Q_WS_MAC
buttonOption.
state |
= QStyle::State_Raised;
#endif
buttonOption.
state &= ~
QStyle::State_HasFocus;
buttonOption.rect = option.rect ;
buttonOption.palette = option.palette;
m_view
->style
()->drawControl
(QStyle::CE_PushButton,
&buttonOption, painter, m_view
);
static const int i = 9; // ### hardcoded in qcommonstyle.cpp
branchOption.
rect = QRect(r.
left() + i
/2, r.
top() + (r.
height() - i
)/2, i, i
);
branchOption.palette = option.palette;
branchOption.
state = QStyle::State_Children;
// draw text
QRect textrect
= QRect(r.
left() + i
*2, r.
top(), r.
width() - ((5*i
)/2), r.
height());
QString text
= elidedText
(option.
fontMetrics, textrect.
width(), Qt
::ElideMiddle,
info .name());
m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter,
option.palette, m_view->isEnabled(), text);
}
}
void MyDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const QAbstractItemModel *model = index.model();
Q_ASSERT(model);
if (qVariantCanConvert<MyCustomsClass>(index.data(MyModel::DeviceInfoRole)))
{
MyCustomsClass info = qVariantValue<MyCustomsClass>(index.data(MyModel::DeviceInfoRole));
QStyleOptionButton buttonOption;
buttonOption.state = option.state;
#ifdef Q_WS_MAC
buttonOption.state |= QStyle::State_Raised;
#endif
buttonOption.state &= ~QStyle::State_HasFocus;
buttonOption.rect = option.rect ;
buttonOption.palette = option.palette;
buttonOption.features = QStyleOptionButton::None;
m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view);
QStyleOption branchOption;
static const int i = 9; // ### hardcoded in qcommonstyle.cpp
QRect r = option.rect;
branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
branchOption.palette = option.palette;
branchOption.state = QStyle::State_Children;
// draw text
QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle,
info .name());
m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter,
option.palette, m_view->isEnabled(), text);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks