PyQt4 - Widget Is Not Shown
I've made this program in Python and Qt4.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
color = QtGui.QColor(99, 0, 0)
class colorButton(QtGui.QWidget):
def __init__(self, args):
QtGui.QWidget.__init__(self,args)
self.setGeometry(150, 22, 50, 50)
self.setStyleSheet("QWidget { background-color: %s }" % color.name())
class ColorDialog(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(40, 40, 220, 100)
self.setWindowTitle('ColorDialog')
button=colorButton(self)
app = QtGui.QApplication(sys.argv)
cd = ColorDialog()
cd.show()
app.exec_()
The intrpreter doesn't give me any error, but the "colored" widget i开发者_如何学Csn't shown. Why? thank
Your class colorButton
inherits from QWidget
, yet you are calling QPushButton.__init__()
in the constructor. Maybe you want it to inherit from QPushButton
?
By using the following class definition, your code works for me:
class colorButton(QtGui.QPushButton):
def __init__(self, *args):
QtGui.QPushButton.__init__(self, *args)
self.setGeometry(150, 22, 50, 50)
self.setStyleSheet("QWidget { background-color: %s }" % color.name())
You need to give the widget a paintEvent.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
color = QtGui.QColor(99, 0, 0)
class colorButton(QtGui.QWidget):
def __init__(self, args):
QtGui.QWidget.__init__(self,args)
self.setGeometry(150, 22, 50, 50)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.fillRect(event.rect(), color)
class ColorDialog(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(40, 40, 220, 100)
self.setWindowTitle('ColorDialog')
button=colorButton(self)
app = QtGui.QApplication(sys.argv)
cd = ColorDialog()
cd.show()
app.exec_()
Try setting autoFillBackground to True before you change the color (before setStylesheet call). AND I think you need to set the pallete. This comment assumes that you meant "the color of the widget is not shown". Please review the syntax as the one illustrated below is for Qt4.3 and I didn't check the latest one. After you set the pallet, there is no need to set the stylesheet.
class colorButton(QtGui.QWidget)
def __init__(self, args):
QtGui.QPushButton.__init__(self,args)
self.setGeometry(150, 22, 50, 50)
self.setAutoFillBackground(True)
plt = QtGui.QPalette()
plt.setColor(QtGui.QPalette.Active,QtGui.QPalette.Window,color)
plt.setColor(QtGui.QPalette.Inactive,QtGui.QPalette.Window,color)
plt.setColor(QtGui.QPalette.Disabled,QtGui.QPalette.Window,color
self.setPalette(plt)
#self.setStyleSheet("QWidget { background-color: %s }" % color.name())
I think you need to give your ColorDialog a Layout using
self.setLayout(SOME_LAYOUT)
then add your button to the layout with something like
self.layout().addItem(button)
Otherwise I am not sure if simply giving your button the ColorDialog as parent is sufficient for display.
精彩评论