[PySide] QAbstractTableModel headerData() Qt.BackgroundRole - Ignored?

Cristián Maureira-Fredes Cristian.Maureira-Fredes at qt.io
Mon Apr 12 00:03:16 CEST 2021


Hello,

Nothing to worry about C++,
if you have a QTableView object, you can just use:

.setStyleSheet("QHeaderView::section{background-color: green;}")

on the object.

For example,
modifying the example:

https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/widgets/itemviews/addressbook

Precisely the "addreswidget.py" file, adding the same line
under the declaration "tableView = QTableView()"

Produces:

https://imgur.com/a/iWo5rdM


Cheers


PS: There is a small tutorial regarding stylesheet on the docs:
https://doc.qt.io/qtforpython/tutorials/basictutorial/widgetstyling.html

On 4/9/21 10:23 PM, Ed Lipson wrote:
> I tried 
> - self.my_tableView.horizontalHeader().setStyleSheet("background-color: 
> gray"), no effect. I get a little lost with the c++ to python 
> translations. I tried ("{background-color: gray;}") too and that did not 
> work either.
> 
> I saw a post about using a QPalette, but I'm not sure how that would 
> help or be applied.
> 
> Thanks,
> Ed
> 
> 
> On Fri, Apr 9, 2021 at 3:43 PM Ed Lipson <edlipsongm at gmail.com 
> <mailto:edlipsongm at gmail.com>> wrote:
> 
>     Then why would there be a background role call to the headerData
>     method? I'll give the stylesheet a try. Maybe I can understand it.
> 
>     Thanks,
>     Ed
> 
> 
>     On Fri, Apr 9, 2021 at 8:04 AM Hans Jörg Maurer <hjm at pmeonline.net
>     <mailto:hjm at pmeonline.net>> wrote:
> 
>         I think you have to use
> 
>         .setStyleSheet(QHeaderView:section{background - color: green})
>         Take a look at https://doc.qt.io/archives/qt-5.9/stylesheet.html
>         <https://doc.qt.io/archives/qt-5.9/stylesheet.html>
> 
>         Regards
>         Hans
> 
> 
> 
>         Am Freitag, den 09.04.2021 um 13:29 schrieb Ed Lipson:
> 
>             How would one set the background color of the QTableView
>             (QtHorizontal orientation) header? I have tried the 'mail
>             title' action, where I see the role being used. I just don't
>             get the color.
> 
>             I used the same concept in data() and the cells take on the
>             background color as appropriate for the state.
> 
>             PySide2 5.15.2 Python3.9.1
> 
>             Thanks,
>             Ed
> 
>             PS: Where is it appropriate to ask such questions? JIRA does
>             not have a question type defined and doing it there may be
>             too much noise in the bug tracking system.
> 
> 
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> https://lists.qt-project.org/listinfo/pyside
> 

-- 
Dr. Cristián Maureira-Fredes
R&D Manager

The Qt Company GmbH
Erich-Thilo-Str. 10
D-12489 Berlin

Geschäftsführer: Mika Pälsi,
Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht
Charlottenburg, HRB 144331 B


More information about the PySide mailing list