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.
I've designed an example layout and program as follows;
The Layout in Qt Designer...
The generated layout 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>
The Code...
#!/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()
The Questions
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.
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?
Question 2 : Is this a limitation of the Dialog or am I implementing this incorrectly to reach my goal?
The
currentColorChanged
signal passes the curently selected color as an argument, so you can just use that:The
curentColor
property of the dialog only gets updated when the dialog is confirmed.