PDA

View Full Version : stylesheets and drawControl(CE_PushButton)



kochelmonster
19th March 2011, 05:06
I am struggling with a qt behavior which is either a bug or a feature.
I wrote a little pyqt program for demonstration.

import PyQt4.QtGui as gui
import sys

class TestButton1(gui.QPushButton):
def paintEvent(self, event):
painter = gui.QStylePainter(self)
option = gui.QStyleOptionButton()
self.initStyleOption(option)
painter.drawControl(gui.QStyle.CE_PushButton, option)


class TestButton2(gui.QPushButton):
def paintEvent(self, event):
painter = gui.QStylePainter(self)
option = gui.QStyleOptionButton()
self.initStyleOption(option)
style = self.style()
style.drawControl(gui.QStyle.CE_PushButton, option, painter)


if __name__ == "__main__":
app = gui.QApplication(sys.argv)
app.setStyleSheet("QPushButton {background-color: #ff0000;}")

frame = gui.QFrame()
frame.setLayout(gui.QVBoxLayout())
frame.layout().addWidget(TestButton1(text="test1"))
frame.layout().addWidget(TestButton2(text="test2"))
frame.show()

app.exec_()

The program shows a small window with two push buttons. The only difference between the buttons is the call of drawControl.

In linux both buttons are rendered with a red background.
In windows the "test1" is rendered red, but "test2" is rendered with the default colour.

Originally I wanted to "steal" the style of a QPushButton to draw it inside an ItemDelegator.

wysota
20th March 2011, 23:26
I would say this is caused by the fact that the call to style (and not to the painter) omits the global stylesheet or even all stylesheets. Stylesheets are implemented as a sort of style proxy. Since you are calling the style directly, the call is probably not "proxied" through the button's style settings. It's an interesting "feature" although it's just a guess, I may be wrong. Try passing "self" as the last argument to drawControl, it might help by informing the style about the widget being painted.