you can subclass QStyledItemDelegate and in re implement paintEvent as
void styledListItemDelegate
::paint(QPainter* painter,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
initStyleOption(&myOpt,index);
//for drawing the checkbox
QRect chkRect
(option.
rect.
right() - option.
rect.
height(),
option.rect.top(),
option.rect.height(),
option.rect.height());
opt.rect = chkRect;
if(option.
state & QStyle::State_Selected)
&opt, painter);
//adjust the rect for displayed text
int dw = -chkRect.width();
myOpt.rect.adjust(0,0,dw,0);
QStyledItemDelegate::paint(painter, myOpt, index);
}
void styledListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionViewItem myOpt = option;
initStyleOption(&myOpt,index);
//for drawing the checkbox
QStyleOptionButton opt;
QRect chkRect(option.rect.right() - option.rect.height(),
option.rect.top(),
option.rect.height(),
option.rect.height());
opt.rect = chkRect;
if(option.state & QStyle::State_Selected)
opt.state = QStyle::State_On;
QApplication::style()->drawControl(QStyle::CE_CheckBox,
&opt, painter);
//adjust the rect for displayed text
int dw = -chkRect.width();
myOpt.rect.adjust(0,0,dw,0);
QStyledItemDelegate::paint(painter, myOpt, index);
}
To copy to clipboard, switch view to plain text mode
set the item delegate for the list widget in your code
listWidget->setItemDelegate(new styledListItemDelegate());
listWidget = new QListWidget();
listWidget->setItemDelegate(new styledListItemDelegate());
new QListWidgetItem(tr("Oak"), listWidget);
new QListWidgetItem(tr("Fir"), listWidget);
new QListWidgetItem(tr("Pine"), listWidget);
To copy to clipboard, switch view to plain text mode
Please note that I have shown code for enabling checkboxes on the left side of the text you need to write code to handle stuff like switching on and off the checked state etc.
Hope this helps
-Sukesh
Bookmarks