开发者

PyQt and QtDesigner

I am trying to implement a LED in PyQt named disLDR1 by trying to change the background color. I am trying to use QPalette to change the BackgroundRole. But what is the equivalent of Qt::red?

Is this the correct way to set the background color or is there any other way?

#!/usr/bin/python -d

import sys
from PyQt4 import QtCore, QtGui
from main import Ui_Form
from PyQt4.QtGui import QPalette

class Top(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        for i in 1, 10:
            self.setOff()
            self.setOn()
    def setOff(self):
        self.pal = QPalette(self.ui.disLDR1.palette())
        self.pal.setColor(self.ui.disLDR1.backgroundRole(), <<<RED COLOR>>>)
        self.ui.disLDR1.setPalette(pal)
    def setOn(self):
        self.pal = QPalette(self.ui.disLDR1.palette())
        self.pal.setColor(self.ui.disLDR1.backgroundRole(), <<<GREEN COLOR>>>)
        self.ui.disLDR1.setPalette(pal)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = Top()
    myapp.show()
    sys.exit(app.exec_())
开发者_开发百科

EDIT: I have been trying to take help from In Qt, how do I set the background color of a widget like combobox or double spin box? but don't know what to substitute for QT::red


You can find the list of predefined Qt color objects at this link. In this case you would just need to use QtCore.Qt.red and QtCore.Qt.blue. You could also use the QColor class to generate arbitrary colors.

It's a matter of preference, but I personally think the easiest and most powerful way to go would be to use a style sheet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜