python - (SOLVED) Using PySide and getting a "live update" from QtGui.QColorDialog -
i have qmainwindow application in i'd change background colour of qgraphicsview in real time, using pyside , qtgui.qcolordialog.
i've designed example layout , program 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 pyside import qtgui, qtcore 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, setupstuff
function sets stuff up, graphics scene , views, , sets qtgui.qcolor object called color_chooser
, currentcolorchanged
signal connected livecolor
function.
i'm printing values terminal @ moment, not updating background...
question 1 : why when i'm changing colour selection, value of self.colour_chooser.currentcolor()
static until click ok
in colour selection dialog?
question 2 : limitation of dialog or implementing incorrectly reach goal?
the currentcolorchanged
signal passes curently selected color argument, can use that:
... def livecolor(self, color): print(color) ...
the curentcolor
property of dialog gets updated when dialog confirmed.
Comments
Post a Comment