我只是在pyside(python / Qt)中尝试我的第一个原型。应用程序本身启动正常,根据我的布局创建一个带小部件的窗口。线程启动并执行,一切正常。除了...
我想通过添加一些指示线程执行状态的自定义窗口小部件来增强GUI。所以我认为闪烁的LED会很好。为此,我尝试实现自定义LED小部件。
请记住,我目前正在尝试学习 python,因此可能会有一些奇怪的方法。无论如何,这是目前状态下的LED小部件类:
from PySide import QtCore, QtGui
class LED(QtGui.QWidget):
class Mode:
STATIC_OFF = 0
STATIC_ON = 1
FLASH_SLOW = 2
FLASH_MEDIUM = 2
FLASH_FAST = 3
class Color:
BLACK = '#000000'
GREEN = '#00FF00'
RED = '#FF0000'
BLUE = '#0000FF'
YELLOW = '#FFFF00'
WHITE = '#FFFFFF'
mode = Mode.STATIC_ON
color = Color.BLACK
radius = 10
status = False
timer = None
outdated = QtCore.Signal()
def __init__(self, mode, color, radius, parent=None):
super(LED, self).__init__(parent)
self.outdated.connect(self.update)
self.setMode(mode,False)
self.setColor(color,False)
self.setRadius(radius,False)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.adjustAppearance)
self.adjustAppearance()
def getCenter(self):
return QtCore.QPoint(self.radius, self.radius)
def getBox(self):
return QtCore.QRect(self.radius, self.radius)
def setColor(self, color, update=True):
assert color in (self.Color.GREEN,self.Color.RED,self.Color.BLUE,self.Color.YELLOW,self.Color.WHITE), "invalid color"
self.color = color
if update:
self.adjustAppearance()
def setMode(self, mode, update=True):
assert mode in (self.Mode.STATIC_OFF,self.Mode.STATIC_ON,self.Mode.FLASH_SLOW,self.Mode.FLASH_MEDIUM,self.Mode.FLASH_FAST),"invalid mode"
self.mode = mode
if update:
self.adjustAppearance()
def setRadius(self, radius, update=True):
assert isinstance(radius, int), "invalid radius type (integer)"
assert 10<=radius<=100, "invalid radius value (0-100)"
self.radius = radius
if update:
self.adjustAppearance()
def switchOn(self):
self.status = True
self.adjustAppearance()
def switchOff(self):
self.status = False
self.adjustAppearance()
def adjustAppearance(self):
if self.mode is self.Mode.STATIC_OFF:
self.status = False
self.timer.stop()
elif self.mode is self.Mode.STATIC_ON:
self.status = True
self.timer.stop()
elif self.mode is self.Mode.FLASH_SLOW:
self.status = not self.status
self.timer.start(200)
elif self.mode is self.Mode.FLASH_SLOW:
self.status = not self.status
self.timer.start(500)
elif self.mode is self.Mode.FLASH_SLOW:
self.status = not self.status
self.timer.start(1000)
self.outdated.emit()
def paintEvent(self, event):
painter = QtGui.QPainter()
painter.begin(self)
self.drawWidget(event, painter)
painter.end()
def drawWidget(self, event, painter):
if self.status:
shade = QtGui.QColor(self.color).darker
else:
shade = QtGui.QColor(self.color).lighter
#painter.setPen(QtGui.QColor('black'), 1, QtCore.Qt.SolidLine)
painter.setPen(QtGui.QColor('black'))
painter.setBrush(QtCore.Qt.RadialGradientPattern)
painter.drawEllipse(self.getCenter(), self.radius, self.radius)
我的问题是,当我将它添加到Windows布局时,窗口小部件根本不会显示。其他小部件(非客户,普通Qt小部件)确实显示,所以我猜这不是创建小部件的问题,而不是我如何使用小部件的问题。然而,如果小部件:
,这是(缩短的)实例class View(QtGui.QMainWindow):
ui = None
def __init__(self, config, parent=None):
log.debug("window setup")
self.config = config
super(View, self).__init__(parent)
try:
self.ui = self.Ui(self)
self.setObjectName("appView")
self.setWindowTitle("AvaTalk")
self.show()
except RuntimeError as e:
log.error(e.message)
class Ui(QtCore.QObject):
# [...]
iconDetector = None
buttonDetector = None
# [...]
def __init__(self, window, parent=None):
log.debug("ui setup")
super(View.Ui, self).__init__(parent)
self.window = window
# central widget
log.debug("widget setup")
self.centralWidget = QtGui.QWidget()
self.widgetLayout = QtGui.QVBoxLayout(self.centralWidget)
# create toolbars
#self.createMenubar()
#self.createCanvas()
self.createToolbar()
#self.createStatusbar()
# visualize widget
self.window.setCentralWidget(self.centralWidget)
# actions
log.debug("actions setup")
self.actionQuit = QtGui.QAction(self.window)
self.actionQuit.setObjectName("actionQuit")
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
log.debug("connections setup")
QtCore.QObject.connect(self.actionQuit, QtCore.SIGNAL("activated()"), self.window.close)
QtCore.QMetaObject.connectSlotsByName(self.window)
def createToolbar(self):
log.debug("toolbar setup")
self.toolbar = QtGui.QHBoxLayout()
self.toolbar.setObjectName("toolbar")
self.toolbar.addStretch(1)
# camera
# detector
self.iconDetector = LED(LED.Mode.STATIC_OFF,LED.Color.GREEN,10,self.window)
self.buttonDetector = IconButton("Detector", "detector",self.window)
self.toolbar.addWidget(self.iconDetector)
self.toolbar.addWidget(self.buttonDetector)
self.toolbar.addStretch(1)
# analyzer
# extractor
# layout
self.widgetLayout.addLayout(self.toolbar)
使用QPainter的实际绘画可能仍然是无稽之谈。我还没有测试过:实际上在测试时我发现isVisible()
在设置完成后在窗口小部件上返回False
。所以我想我错过了一个中心点。不幸的是我无法找到我想念的东西......
也许有人能发现我的问题?谢谢!
答案 0 :(得分:2)
在实施从QWidget
派生的自定义窗口小部件时要注意的一件事是sizeHint
或minimumSizeHint
QWidget
默认返回无效QSize
。这意味着,如果将其添加到布局中,则根据其他窗口小部件,它将缩小为0
。这实际上使它“不可见”。虽然isVisible
仍会返回True
。窗口小部件是“可见的”,但它没有任何要显示的内容(0大小)。所以,如果你得到False
,肯定还有另一个问题。
因此有必要用合理的大小来定义这两种方法:
class LED(QtGui.QWidget):
# ...
def sizeHint(self):
size = 2 * self.radius + 2
return QtCore.QSize(size, size)
def minimumSizeHint(self):
size = 2 * self.radius + 2
return QtCore.QSize(size, size)
注意:还有其他问题:
mode
,color
等定义为类属性,然后使用实例属性覆盖它们。他们不会破坏任何东西,但他们毫无意义。painter.setBrush(QtCore.Qt.RadialGradientPattern)
错了。您无法使用QtCore.Qt.RadialGradientPattern
创建画笔。它在那里,所以brush.style()
可以返回一些东西。如果您想要渐变图案,则应使用QGradient
constructor创建画笔。if self.mode is self.Mode.STATIC_OFF
:与is
比较是错误的。 is
比较身份,您需要==
。 (另外,FLASH_SLOW
和FLASH_MEDIUM
都是2
)assert
用于调试和单元测试。你不应该在实际代码中使用它。如果你愿意,可以提出异常。或者有明智的default
s,其中无效值将被替换为。{/ li>