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_()
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_()
To copy to clipboard, switch view to plain text mode
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.
Bookmarks