我有一个 QMainWindow 应用程序,我想在其中使用 PySide 和 QtGui.QColorDialog 实时更改 QGraphicsView 的背景颜色。
我设计了一个示例布局和程序,如下所示;
Qt 设计器中的布局...
生成的布局 XML...
<?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()
问题
运行时,该setupStuff
函数基本上设置了一些东西,例如图形场景和视图,还设置了一个名为 的 QtGui.QColor 对象color_chooser
,其currentColorChanged
信号连接到该liveColor
函数。
我现在只是将值打印到终端,而不是更新背景......
问题 1:为什么当我更改颜色选择时,在我单击颜色选择对话框self.colour_chooser.currentColor()
之前,值始终是静态的?OK
问题 2:这是对话框的限制,还是我错误地实现了这个以达到我的目标?