gpt4 book ai didi

python - 使用 PySide 并从 QtGui.QColorDialog 获取 "live update"

转载 作者:太空宇宙 更新时间:2023-11-03 18:49:45 25 4
gpt4 key购买 nike

我有一个 QMainWindow 应用程序,我想在其中使用 PySide 和 QtGui.QColorDialog 实时更改 QGraphicsView 的背景颜色。

我设计了一个示例布局和程序如下;

Qt Designer 中的布局...

enter image description here

生成的布局 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函数基本上会设置一些东西,比如图形场景和 View ,并且还设置一个名为color_chooser的QtGui.QColor对象,其currentColorChanged 信号连接到 liveColor 函数。

我现在只是将值打印到终端,而不是更新背景...

问题 1:为什么当我更改颜色选择时,self.colour_chooser.currentColor() 的值始终是静态的,直到我单击确定 在颜色选择对话框中?

问题 2:这是对话框的限制还是我为了实现目标而错误地实现了此操作?

最佳答案

currentColorChanged 信号将当前选择的颜色作为参数传递,因此您可以使用它:

...

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

对话框的 currentColor 属性仅在对话框被确认时更新。

关于python - 使用 PySide 并从 QtGui.QColorDialog 获取 "live update",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18617708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com