Widgets: Set viewItemPosition style option for QTableView and QListView

Before this patch, we did not set the view item positions in the views.
This was fine as they were ignored until
b780eaf6a063a7efe03417cf6b7008a188e222a4 added a condition to early exit
on invalid positions. This then broke all qss background styling using
QStyleOptionViewItem::ViewItemPosition as they were always invalid.

Set the position when trying to draw a cell of the view before
reaching the code handling the qss rules for backgrounds.

Fixes: QTBUG-137346
Pick-to: 6.8
Change-Id: I83d7a3ea7b9bab98889791bb807988a74e355b93
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
(cherry picked from commit 5aa1bc46217dc6f53fde8fe17608cf0e5f7a5e78)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit da9ac3bf1d2c2d7259563918f5e665d421957ec5)
This commit is contained in:
Olivier De Cannière 2025-06-06 15:20:47 +02:00 committed by Qt Cherry-pick Bot
parent d86dc86a93
commit 778805a66f
6 changed files with 73 additions and 0 deletions

View File

@ -980,6 +980,7 @@ void QListView::initViewItemOption(QStyleOptionViewItem *option) const
if (d->gridSize().isValid()) {
option->rect.setSize(d->gridSize());
}
option->viewItemPosition = QStyleOptionViewItem::OnlyOne;
}

View File

@ -1022,6 +1022,25 @@ void QTableViewPrivate::sortIndicatorChanged(int column, Qt::SortOrder order)
model->sort(column, order);
}
QStyleOptionViewItem::ViewItemPosition QTableViewPrivate::viewItemPosition(
const QModelIndex &index) const
{
int visualColumn = horizontalHeader->visualIndex(index.column());
int count = horizontalHeader->count();
if (count <= 0 || visualColumn < 0 || visualColumn >= count)
return QStyleOptionViewItem::Invalid;
if (count == 1 && visualColumn == 0)
return QStyleOptionViewItem::OnlyOne;
else if (visualColumn == 0)
return QStyleOptionViewItem::Beginning;
else if (visualColumn == count - 1)
return QStyleOptionViewItem::End;
else
return QStyleOptionViewItem::Middle;
}
/*!
\internal
Draws a table cell.
@ -1045,6 +1064,7 @@ void QTableViewPrivate::drawCell(QPainter *painter, const QStyleOptionViewItem &
}
opt.palette.setCurrentColorGroup(cg);
}
opt.viewItemPosition = viewItemPosition(index);
if (index == q->currentIndex()) {
const bool focus = (q->hasFocus() || viewport->hasFocus()) && q->currentIndex().isValid();

View File

@ -138,6 +138,8 @@ public:
return horizontalHeader->logicalIndex(visualCol);
}
QStyleOptionViewItem::ViewItemPosition viewItemPosition(const QModelIndex &index) const;
inline int accessibleTable2Index(const QModelIndex &index) const {
const int vHeader = verticalHeader ? 1 : 0;
return (index.row() + (horizontalHeader ? 1 : 0)) * (index.model()->columnCount() + vHeader)

View File

@ -0,0 +1,3 @@
QAbstractItemView::item:only-one{
background-color: red;
}

View File

@ -0,0 +1,9 @@
QAbstractItemView::item:first{
background-color: green;
}
QAbstractItemView::item:middle {
background-color: yellow;
}
QAbstractItemView::item:last {
background-color: red;
}

View File

@ -30,6 +30,12 @@ private slots:
void tst_QHeaderView_data();
void tst_QHeaderView();
void tst_QTableView_data();
void tst_QTableView();
void tst_QListView_data();
void tst_QListView();
private:
QDir styleSheetDir;
};
@ -227,6 +233,38 @@ void tst_Stylesheet::tst_QHeaderView()
QBASELINE_TEST(takeSnapshot());
}
void tst_Stylesheet::tst_QTableView_data()
{
loadTestFiles();
}
void tst_Stylesheet::tst_QTableView()
{
QHBoxLayout *layout = new QHBoxLayout;
QTableWidget *tw = new QTableWidget(2, 3);
layout->addWidget(tw);
testWindow()->setLayout(layout);
makeVisible();
QBASELINE_TEST(takeSnapshot());
}
void tst_Stylesheet::tst_QListView_data()
{
loadTestFiles();
}
void tst_Stylesheet::tst_QListView()
{
QStringListModel m({ "Berlin", "Paris", "London" });
QListView *v = new QListView;
v->setModel(&m);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(v);
testWindow()->setLayout(layout);
makeVisible();
QBASELINE_TEST(takeSnapshot());
}
QBASELINETEST_MAIN(tst_Stylesheet)
#include "tst_baseline_stylesheet.moc"