Reputation: 1734
I'm about to implement a cross-platform Qt program. I have one problem I don't understand: when the program is executed on Windows, I get the wrong background color for some widgets. In the first place, I wrote the program in C++ with the very same results, so I think this is not a Python specific problem.
I stripped my code down to a simple program that shows the "error". I'm not sure which widget causes it, so I simply included all elements that are used in the real program.
Here's how it looks on Linux:
And here's the Windows rendering with the wrong background:
Here's the code:
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QVBoxLayout, QTabWidget, QWidget, \
QGroupBox, QVBoxLayout, QScrollArea, QFrame, QHBoxLayout, \
QGridLayout, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
mainWidget = QWidget(self)
layout = QVBoxLayout(mainWidget)
self.setCentralWidget(mainWidget)
self.tabWidget = QTabWidget(self)
layout.addWidget(self.tabWidget)
self.scorePage = ScorePage()
self.tabWidget.addTab(self.scorePage, 'Some Tab')
self.scorePage.reload()
class GameWidget(QWidget):
def __init__(self):
super(GameWidget, self).__init__()
layout = QGridLayout(self)
boogerLabel = QLabel(self)
boogerLabel.setText('Some Text')
layout.addWidget(boogerLabel, 0, 0)
class ScorePage(QWidget):
def __init__(self):
super(ScorePage, self).__init__()
layout = QVBoxLayout(self)
self.scoreBox = QGroupBox(self)
self.scoreBoxLayout = QVBoxLayout(self.scoreBox)
layout.addWidget(self.scoreBox)
self.scoreBoxScroll = QScrollArea(self.scoreBox)
self.scoreBoxScroll.setFrameShape(QFrame.NoFrame)
self.scoreBoxLayout.addWidget(self.scoreBoxScroll)
self.scoreBoxWidget = QWidget(self)
def reload(self):
self.scoreBoxWidget.deleteLater()
self.scoreBoxWidget = QWidget(self)
self.scoreBoxWidgetLayout = QHBoxLayout(self.scoreBoxWidget)
newGame = GameWidget()
self.scoreBoxWidgetLayout.addWidget(newGame)
self.scoreBoxScroll.setWidget(self.scoreBoxWidget)
self.scoreBoxWidget.setAutoFillBackground(False)
def main(argv):
app = QApplication(sys.argv, True)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
How can I get and set the correct backgrolund color (the one from the QGroupBox)?
Thanks in advance for all help about this!
Upvotes: 1
Views: 324
Reputation: 1734
I finally figured out what's causing this. It's the background of the QScrollArea (which is probably uses the same as the QTabWidget on my Linux installation, so that I didn't see it).
Simply defining the background of the QScrollArea as transparent solves it:
self.scoreBoxScroll.setStyleSheet('QScrollArea { background-color: transparent; }')
Upvotes: 1
Reputation: 71
Hey some time ago I faced with similar problem and I fixed that by adding windows styles which are different than that default:
Here is my main for your example which works as you want:
def main(argv):
app = QApplication(sys.argv, True)
from platform import system
if system() == "Windows":
app.setStyle(QStyleFactory.create("windows"))
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
QStyleFactory is in QtGui so you have to extend your import
Upvotes: 1