[英]Using PySide and getting a “live update” from QtGui.QColorDialog
I have a QMainWindow application in which I'd like to change the background colour of the QGraphicsView in real time, using PySide and QtGui.QColorDialog. 我有一个QMainWindow应用程序,我想使用PySide和QtGui.QColorDialog实时更改QGraphicsView的背景色。
I've designed an example layout and program as follows; 我设计了一个示例布局和程序,如下所示;
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ColourChanger</class>
<widget class="QMainWindow" name="ColourChanger">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>486</width>
<height>558</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>110</x>
<y>150</y>
<width>256</width>
<height>192</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>150</x>
<y>75</y>
<width>171</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Change Background</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>486</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>ColourChanger</receiver>
<slot>buttonPressed()</slot>
<hints>
<hint type="sourcelabel">
<x>235</x>
<y>113</y>
</hint>
<hint type="destinationlabel">
<x>242</x>
<y>278</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>buttonPressed()</slot>
</slots>
</ui>
#!/usr/bin/env python3.3
from PySide import QtGui, QtCore
from ColourChangerMainWindow import Ui_ColourChanger
class MainWindow(QtGui.QMainWindow, Ui_ColourChanger):
def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
QtGui.QMainWindow.__init__(self, parent, f)
self.setupUi(self)
def setupStuff(self):
self.view = self.graphicsView
self.scene = QtGui.QGraphicsScene()
self.scene.setSceneRect(QtCore.QRectF(self.view.viewport().rect()))
self.view.setScene(self.scene)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
self.view.setBackgroundBrush(brush)
self.colour_chooser = QtGui.QColorDialog()
self.colour_chooser.blockSignals(True)
self.colour_chooser.currentColorChanged.connect(self.liveColor)
self.colour_chooser.blockSignals(False)
def buttonPressed(self):
print("buttonPressed colour_chooser= ", self.colour_chooser)
self.colour_chooser.open()
def liveColor(self):
value = self.colour_chooser.currentColor()
print(value)
print("liveColor colour_chooser= ", self.colour_chooser)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.setupStuff()
window.show()
app.exec_()
app.deleteLater()
sys.exit()
When run, the setupStuff
function basically sets stuff up, like the graphics scene and views, and also sets up a QtGui.QColor object called color_chooser
, whose currentColorChanged
signal is connected to the liveColor
function. 运行时, setupStuff
函数基本上会设置诸如图形场景和视图之类的内容,并且还会设置一个名为color_chooser
的QtGui.QColor对象,该对象的currentColorChanged
信号连接到liveColor
函数。
I'm just printing the values to the terminal at the moment, not updating the background... 我现在只是将值打印到终端,而不是更新背景...
Question 1 : Why when I'm changing the colour selection, the value of self.colour_chooser.currentColor()
is always static until I click OK
in the colour selection dialog? 问题1 :为什么在更改颜色选择时, self.colour_chooser.currentColor()
值始终是静态的,直到我在颜色选择对话框中单击“ OK
为止?
Question 2 : Is this a limitation of the Dialog or am I implementing this incorrectly to reach my goal? 问题2 :这是对话框的限制,还是我为实现目标而错误地实现了此目的?
The currentColorChanged
signal passes the curently selected color as an argument, so you can just use that: currentColorChanged
信号将当前选择的颜色作为参数传递,因此您可以使用该颜色:
...
def liveColor(self, color):
print(color)
...
The curentColor
property of the dialog only gets updated when the dialog is confirmed. 仅在确认对话框后,对话框的curentColor
属性才会更新。
Thanks to @mata above for answering the question. 感谢上面的@mata回答了这个问题。
Here's the updated code, which now updates the background in the QGraphicsView. 这是更新的代码,现在可以更新QGraphicsView中的背景。 I've also added an initial black colour to colour_chooser in the setup routine... 我还在设置例程中为colour_chooser添加了初始黑色...
#!/usr/bin/env python3.3
from PySide import QtGui, QtCore
from ColourChangerMainWindow import Ui_ColourChanger
class MainWindow(QtGui.QMainWindow, Ui_ColourChanger):
def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
QtGui.QMainWindow.__init__(self, parent, f)
self.setupUi(self)
def setupStuff(self):
self.view = self.graphicsView
self.scene = QtGui.QGraphicsScene()
self.scene.setSceneRect(QtCore.QRectF(self.view.viewport().rect()))
self.view.setScene(self.scene)
self.brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
self.brush.setStyle(QtCore.Qt.SolidPattern)
self.view.setBackgroundBrush(self.brush)
self.colour_chooser = QtGui.QColorDialog()
self.colour_chooser.setCurrentColor(QtGui.QColor(0, 0, 0))
self.colour_chooser.blockSignals(True)
self.colour_chooser.currentColorChanged.connect(self.liveColor)
self.colour_chooser.blockSignals(False)
def buttonPressed(self):
self.colour_chooser.open()
def liveColor(self, color):
self.view.setBackgroundBrush(color)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.setupStuff()
window.show()
app.exec_()
app.deleteLater()
sys.exit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.