Using PySide and getting a "live update" from QtGui.QColorDialog

2.3k views Asked by At

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...

enter image description here

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?

2

There are 2 answers

1
mata On BEST ANSWER

The currentColorChanged signal passes the curently selected color as an argument, so you can just use that:

...

def liveColor(self, color):
    print(color)
    ...

The curentColor property of the dialog only gets updated when the dialog is confirmed.

1
Broken Man On

Thanks to @mata above for answering the question.

Here's the updated code, which now updates the background in the QGraphicsView. I've also added an initial black colour to colour_chooser in the setup routine...

#!/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()

enter image description here