I'm using a listview with a custom made model where the items in the listview are delegates.
I can't seem to get clicking on the list view to trigger selection changed. It looks like its because the delegate uses a label to render the information.
Here are some code bits:
This is the paint function in the delegate. The viewPtr is defined elsewhere and is a pointer to the view.
{
painter->save();
std::cout << "Row: " << index.row() << std::endl;
if(viewPtr->indexWidget(index) == NULL)
{
Data *data = qVariantValue<Data*>(index.data(Qt::DisplayRole));
QString displayValue
= data
->displayValue
();
label->setAutoFillBackground(true);
label->setWordWrap(true);
label->setText(displayValue);
if(option.
state & QStyle::State_Selected) {
//painter->fillRect(option.rect, option.palette.highlight());
label->setPalette(option.palette);
label
->setBackgroundRole
(QPalette::Highlight);
viewPtr->setIndexWidget(index, label);
}
else
{
if(index.row() & 1)
{
//painter->fillRect(option.rect, QBrush(QColor("#c0c0c0")));
label->setPalette(p);
label
->setBackgroundRole
(QPalette::Background);
viewPtr->setIndexWidget(index, label);
}
else
{
//painter->fillRect(option.rect, QBrush(QColor("#dddddd")));
label->setPalette(p);
label
->setBackgroundRole
(QPalette::Background);
viewPtr->setIndexWidget(index, label);
}
}
}
painter->restore();
}
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
painter->save();
std::cout << "Row: " << index.row() << std::endl;
if(viewPtr->indexWidget(index) == NULL)
{
QLabel *label = new QLabel();
Data *data = qVariantValue<Data*>(index.data(Qt::DisplayRole));
QString displayValue = data->displayValue();
label->setAutoFillBackground(true);
label->setWordWrap(true);
label->setText(displayValue);
if(option.state & QStyle::State_Selected)
{
//painter->fillRect(option.rect, option.palette.highlight());
label->setPalette(option.palette);
label->setBackgroundRole(QPalette::Highlight);
viewPtr->setIndexWidget(index, label);
QItemDelegate::paint(painter, option, index);
}
else
{
if(index.row() & 1)
{
//painter->fillRect(option.rect, QBrush(QColor("#c0c0c0")));
QPalette p;
p.setBrush(QPalette::Background, QBrush(QColor("#c0c0c0")));
label->setPalette(p);
label->setBackgroundRole(QPalette::Background);
viewPtr->setIndexWidget(index, label);
}
else
{
//painter->fillRect(option.rect, QBrush(QColor("#dddddd")));
QPalette p;
p.setBrush(QPalette::Background, QBrush(QColor("#dddddd")));
label->setPalette(p);
label->setBackgroundRole(QPalette::Background);
viewPtr->setIndexWidget(index, label);
}
QItemDelegate::paint(painter, option, index);
}
}
painter->restore();
}
To copy to clipboard, switch view to plain text mode
I've tried the following to no avail:
MyDelegate delegate;
delegate.setViewPointer(window->listView);
window->listView->setItemDelegate(&delegate);
MyDelegate delegate;
delegate.setViewPointer(window->listView);
window->listView->setItemDelegate(&delegate);
QObject::connect(window->listView, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
&delegate, SLOT(selected(const QModelIndex ¤t, const QModelIndex &)));
To copy to clipboard, switch view to plain text mode
The delegate class has a selected slot that I've defined.
Where am I going wrong? The above code works if I'm not using a label...
Bookmarks