PDA

View Full Version : Emphasizing "current item" in subclass of QAbstractItemView



Davor
2nd April 2010, 14:07
By default, there is some barely visible dotted rectangle around the
QItemSelectionModel::currentIndex (). Has anyone suggestions how to change
this efficiently. (i.e. I think adjusting the model data with setData() and
Qt::FontRole or Qt::BackgroundRole or something similar isn't the right way
to do this.)

So has anyone any other suggestions?

Kind regards,
Davor

Lykurg
2nd April 2010, 14:25
set your own delegate and reimplement its paint method. There simply remove QStyle::State_Selected from the QStyleOptionViewItem and pass all argument the the base class.
(There are some threads about that topic in the forum)

Davor
2nd April 2010, 15:12
Thanks Lykurg.

I'll try your suggestion next week. I also couldn't find any relevant post about the "current" item. I did find posts about “selected” item(s), but that's not the issue here.

Lykurg
2nd April 2010, 21:08
Damn, everytime I came into that trouble with QStyle::State_HasFocus and QStyle::State_Selected....

The dotted line is the result of one or these two states. So in the "selected items threads" you will find how to remove these flag inside the paint event. And there is the connection to your "current item" problem ;)

Lykurg
2nd April 2010, 21:21
I hope I remember next time:p
#include <QtGui>

class MyDelegate : public QItemDelegate
{
public:
MyDelegate(QObject* parent): QItemDelegate(parent)
{}

virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionViewItem opt;
opt = const_cast<QStyleOptionViewItem&>(option);
opt.state &= ~QStyle::State_HasFocus;
QItemDelegate::paint(painter, opt, index);
}
};

int main(int argc, char** argv)
{
QApplication app(argc, argv);

QTableView view;
MyDelegate delegate(&view);
view.setItemDelegate(&delegate);

QStandardItemModel model;
model.setRowCount(10);
model.setColumnCount(10);
for (int row = 0; row < model.rowCount(); ++row)
{
for (int column = 0; column < model.columnCount(); ++column)
{
QStandardItem* item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}
view.setModel(&model);

view.show();
return app.exec();
}

Lykurg
2nd April 2010, 21:26
Ehm.. and change is not remove! But once you have removed it, you can paint your own emphasized border. If you want to change it directly, you have to subclass QStyle and write your own routine there. But that is more complex (QProxyStyle).