gpt4 book ai didi

python - PyQt5 根据输入值自动绘制

转载 作者:行者123 更新时间:2023-12-01 08:52:11 24 4
gpt4 key购买 nike

我最近使用Qpaint创建了一个小部件,我想将值传递给它,同时强制Qpaint小部件从输入值中绘制。这个想法是从Qdialog定义一个数据值并将其传递给主窗口小部件,然后将该值传递给Qpaint Widget类。我希望,当用户单击“获取值”按钮时,会出现一个对话框小部件并插入一些 int 值,然后将其传递给主小部件。从那里将值传递给正确的Paint类。这将绘制并显示结果。我尝试使用Qlabel,首先将值分配给Qlabel或QlineEdit

class Button(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Button, self).__init__(parent)
---------

self.value = QtWidgets.QLabel()

--------

然后在paint类中调用它们的值或文本。然后将其分配给Qpaint事件。但似乎不起作用。”

class Paint(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Paint, self).__init__(parent)

self.button = Button()
self.Value = self.button.value

---------

painter.drawRect(100,100,250,250) <----- instead of value 250 having self.Value

代码Main.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from datainput import *


class Foo(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Foo, self).__init__(parent)
self.setGeometry(QtCore.QRect(200, 100, 800, 800))

self.button = Button()
self.paint = Paint()

self.lay = QtWidgets.QVBoxLayout()
self.lay.addWidget(self.paint)
self.lay.addWidget(self.button)
self.setLayout(self.lay)

class Paint(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Paint, self).__init__(parent)

self.button = Button()
self.Value = self.button.value

self.setBackgroundRole(QtGui.QPalette.Base)
self.setAutoFillBackground(True)

def paintEvent(self, event):

self.pen = QtGui.QPen()
self.brush = QtGui.QBrush( QtCore.Qt.gray, QtCore.Qt.Dense7Pattern)
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(self.pen)
painter.setBrush(self.brush)
painter.drawRect(100,100,250,250)
painter.setBrush(QtGui.QBrush())

class Button(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Button, self).__init__(parent)

getbutton = QtWidgets.QPushButton('Getting values')
Alay = QtWidgets.QVBoxLayout(self)
Alay.addWidget(getbutton)

self.value = QtWidgets.QLabel()

getbutton.clicked.connect(self.getbuttonfunc)


def getbuttonfunc(self):
subwindow=Dinput()
subwindow.setWindowModality(QtCore.Qt.ApplicationModal)
if subwindow.exec_() == QtWidgets.QDialog.Accepted:
self._output = subwindow.valueEdit.text()
return self.value.setText(self._output)


if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Foo()
w.show()
sys.exit(app.exec_())

输入Qdialog代码,datainput.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Dinput(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Dinput, self).__init__(parent)

valuelabel = QtWidgets.QLabel('Input: ')
self.valueEdit = QtWidgets.QLineEdit()

buttonBox = QtWidgets.QDialogButtonBox()
buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.close)

self.Alay = QtWidgets.QHBoxLayout()
self.Alay.addWidget(valuelabel)
self.Alay.addWidget(self.valueEdit)

self.Blay = QtWidgets.QVBoxLayout()
self.Blay.addLayout(self.Alay)
self.Blay.addWidget(buttonBox)
self.setLayout(self.Blay)

def closeEvent(self, event):
super(Dinput, self).closeEvent(event)

def accept(self):
super(Dinput, self).accept()

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Dinput()
w.show()
sys.exit(app.exec_())

可视化

enter image description here

我很感激任何帮助。谢谢ssss

最佳答案

datainput 无关紧要,您的任务只是获取一个数字,因此对于空间问题我不会使用它,而是使用 QInputDialog::getInt()。直奔问题,在这些随时可以获得值的情况下的策略是通过信号通知其他 View 的变化,在接收到值的槽中是更新一个存储该值的变量并调用更新以便在必要时调用paintEvent,并在paintEvent中使用存储值的变量。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Foo(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Foo, self).__init__(parent)
self.setGeometry(QtCore.QRect(200, 100, 800, 800))

self.button = Button()
self.paint = Paint()
self.button.valueChanged.connect(self.paint.set_size_square)

self.lay = QtWidgets.QVBoxLayout(self)
self.lay.addWidget(self.paint)
self.lay.addWidget(self.button)

class Paint(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Paint, self).__init__(parent)
self.setBackgroundRole(QtGui.QPalette.Base)
self.setAutoFillBackground(True)
self._size_square = 250

@QtCore.pyqtSlot(int)
def set_size_square(self, v):
self._size_square = v
self.update()

def paintEvent(self, event):
pen = QtGui.QPen()
brush = QtGui.QBrush( QtCore.Qt.gray, QtCore.Qt.Dense7Pattern)
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(pen)
painter.setBrush(brush)
r = QtCore.QRect(QtCore.QPoint(100, 100), self._size_square*QtCore.QSize(1, 1))
painter.drawRect(r)

class Button(QtWidgets.QWidget):
valueChanged = QtCore.pyqtSignal(int)

def __init__(self, parent=None):
super(Button, self).__init__(parent)
getbutton = QtWidgets.QPushButton('Getting values')
Alay = QtWidgets.QVBoxLayout(self)
Alay.addWidget(getbutton)
self.value = QtWidgets.QLabel()
getbutton.clicked.connect(self.getbuttonfunc)

@QtCore.pyqtSlot()
def getbuttonfunc(self):
number, ok = QtWidgets.QInputDialog.getInt(self, self.tr("Set Number"),
self.tr("Input:"), 1, 1)
if ok:
self.valueChanged.emit(number)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Foo()
w.show()
sys.exit(app.exec_())

关于python - PyQt5 根据输入值自动绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53037054/

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