PyQt: change base color of checkbox on TreeWidgetItem...

1,097 views
Skip to first unread message

Geoff Harvey

unread,
Oct 16, 2012, 8:27:25 PM10/16/12
to python_in...@googlegroups.com
Y'all-

Is there any way to change the base color of a checkbox on a TreeWidgetItem *without* having to redefine every little aspect of the QTreeWidget/QTreeView?

I can change the color with a stylesheet doing something like this ...

QTreeView::indicator {
    color: rgb(240, 240, 240)
}

... but then I lose my checkbox icons.

This makes me sad.

Any ideas?

Thanks!

geoff

Jay Goodman

unread,
Oct 17, 2012, 10:01:55 PM10/17/12
to python_in...@googlegroups.com
I've also found frustration in QT not having a lot of specific methods for setting colors.  However, you can always overload the paintEvent and repaint the entire widget, but then you have to repaint the entire widget yourself, including the check.  
I've found this tutorial fairly helpful whenever I go down the road of painting: http://zetcode.com/tutorials/pyqt4/drawing/

 def paintEvent(self, ev):

        p = QtGui.QPainter(self)
        
        if not p.isActive():
            p.begin()
        #do your painting here
        ev.ignore()

Justin Israel

unread,
Oct 18, 2012, 12:30:46 AM10/18/12
to python_in...@googlegroups.com
I think in this specific case, the options are either to fully define the indicator states in the stylesheet (if I understood the problem correctly), or to get down and dirty with the QItemDelegate for the QTreeView.
I played with that a bit, but have not found a good way to only change a color component. It is not actually a checkbox widget, but rather a paint style that draws it. You could draw your own checkbox however you want:

QItemDelegate.drawCheck()

You would subclass and implement drawCheck to paint the checkbox however you want. I know it sucks to have to do that much work. If it were actually a tangible QCheckBox widget, then you could just access it and set the palette.



Reply all
Reply to author
Forward
0 new messages