我画了一条线,我想设置颜色和线宽。为了使我的应用程序可自定义,我希望在样式表中具有此属性。我该怎么办?我尝试将QColor
子类化为没有太大成功,因为该行仍然是黑色的。这是我试过的:
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor
class Main(QWidget):
def paintEvent(self, e):
painter = QPainter()
painter.begin(self)
painter.setPen(LineColor())
painter.drawLine(0, 0, 100, 100)
painter.end()
class LineColor(QColor):
''' Exposes a class, so that its color can be set using the style sheet '''
qss = '''
LineColor {
color: red;
}
'''
app = QApplication(sys.argv)
app.setStyleSheet(qss)
main = Main()
main.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
你在尝试什么,恕我直言是不可能的。由于您已经对QMainWindow
进行了细分,为什么不覆盖setStyleSheet
,并解析样式表,并相应地设置颜色?
class Main(QWidget):
def __init__(...) :
...
...
...
self.myLineColor = QColor( Qt::black )
...
...
def setStyleSheet( sheet ) :
# Your code to parse the stylesheet
self.myLineColor.setRgb( ... ) # or setNamedColor or similar.
QMainWindow::setStyleSheet( self, sheet )
def paintEvent(self, e):
painter = QPainter()
painter.begin(self)
painter.setPen(self.myLineColor)
painter.drawLine(0, 0, 100, 100)
painter.end()