D K
D K

Reputation: 5750

PyQt: QStyledItemDelegate in QTableView on Windows Vista/7?

I would like to get a cell/row to look like this in a QTableView:

enter image description here

When running Ubuntu, the QStyledItemDelegate works for both the QTreeView and the QTableView, but on Windows, it only works on QTreeView, and only if I don't reimplement the paint method.

So my 2 questions are:

How do I get the QStyledItemDelegate to look like the above image in a QTableView?

How do I get the QStyledItemDelegate to look like the above image when reimplementing paint?

The image beside the text is not neccessary. I am just looking for the styled hover and selection box. Just in case this is needed, my Qt version is 4.7.2.

Upvotes: 1

Views: 1960

Answers (2)

D K
D K

Reputation: 5750

I finally figured enough of it out.

What I did not solve is why not overriding QStyledItemDelegate.paint() has a different effect from overriding it like:

def paint(self, painter, option, index):
    QStyledItemDelegate.paint(self, painter, option, index)

but that was not part of my question anyway.


What I did solve is how to get the native appearance when painting manually. Previously, when painting the item, I used:

QApplication.style().drawControl(QStyle.CE_ItemViewItem, option, painter)

which had the issue of not painting the native focus or selection. I had a look at the method signature of QApplication.style().drawControl():

void QStyle::drawControl ( ControlElement element, const QStyleOption * option,
                           QPainter * painter, const QWidget * widget = 0 )

and noticed the widget parameter, and tried passing in a QTreeView. It worked. It does not matter what QTreeView is passed, but it makes the view render natively.

So in the end, rendering a native QTableView is as simple as calling:

QApplication.style().drawControl(QStyle.CE_ItemViewItem, option, painter, QTreeView())

in the QStyledItemDelegate's paint method.

Upvotes: 1

nitely
nitely

Reputation: 2288

I've opened a thread on qtcentre asking for the same thing. The guy who answer me, provide me with a code example written in C++ and a picture showing the result. It seems to work fine in C++ but not in PySide (dont know about pyQt), so it may be a bug.

This is the working code:

void ProgressBarDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QStyledItemDelegate::paint(painter, option, index);

if( index.column() == 1 )
{
int progress = index.data().toInt();

QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.rect.setTop( option.rect.top() + 1 );
progressBarOption.rect.setHeight( option.rect.height() - 2 );
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.text = QString::number(progress) + "%";
progressBarOption.textVisible = true;
progressBarOption.textAlignment = Qt::AlignCenter;

QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
}
}

Upvotes: 1

Related Questions