gpt4 book ai didi

python - pyqt 在现有的 GUI 小部件上绘图

转载 作者:太空狗 更新时间:2023-10-30 01:03:23 28 4
gpt4 key购买 nike

我是 pyqt 的新手。我正在做一个程序,它允许你点击图片并记住你点击的点的坐标,并在 GUI 的小部件上画一个火柴人。我的代码现在可以弹出一个新窗口来显示一个有 4 个点的多边形。但是,我希望它可以显示在我已经用pyqt制作的ui文件上。小部件的对象名称称为小部件。我希望有人可以帮助我修改代码以在 gui 小部件上显示多边形而不提示新窗口。

非常感谢!!!

import sys
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from Main_window import *

global imgloc
imgloc = "1.jpg"
array = []
clicks = 0

class MyForm(QtGui.QMainWindow):

def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.local_image = QImage(imgloc)
self.imageLocation = imgloc
self.local_scene = QGraphicsScene()
self.pixMapItem = QGraphicsPixmapItem(QPixmap(self.local_image), None, self.local_scene)
self.ui.graphicsView_5.setScene( self.local_scene )
self.pixMapItem.mousePressEvent = self.pixelSelect

def pixelSelect(self,event):
global imgloc
a = event.pos().x()
b = event.pos().y()
global clicks
global array
if clicks != 4:
clicks += 1
point = QPoint(a,b)
array.append(point)
else:
clicks = 0
dialog = DialogBody()
dialog.show()
dialog.exec_()
array = []

class DialogBody(QDialog):
def __init__(self,parent=None):
super(QDialog,self).__init__(parent)
self.setGeometry(100, 100, QImage(imgloc).height(), QImage(imgloc).width())

def paintEvent(self,e):
qp = QtGui.QPainter()
qp.begin(self)
self.drawBody(qp)
qp.end()

def drawBody(self, qp):
qp.setPen(QtCore.Qt.red)
qp.drawPolygon(array[0],array[1],array[2],array[3])
qp.drawEllipse(array[0],2,2)
qp.drawEllipse(array[1],2,2)
qp.drawEllipse(array[2],2,2)
qp.drawEllipse(array[3],2,2)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp= MyForm()
myapp.show()
sys.exit(app.exec_())

最佳答案

看起来你想在 QGraphicsScene 上绘制项目?在这种情况下,您可以将项目添加到场景中:

#!/usr/bin/env python
import sys

from PyQt4 import QtCore, QtGui


class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.scene = QtGui.QGraphicsScene()
self.view = QtGui.QGraphicsView(self.scene)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('image.png'), None, self.scene)
self.pixmap_item.mousePressEvent = self.pixelSelect
self.click_positions = []

def pixelSelect(self, event):
self.click_positions.append(event.pos())
if len(self.click_positions) < 4:
return
pen = QtGui.QPen(QtCore.Qt.red)
self.scene.addPolygon(QtGui.QPolygonF(self.click_positions), pen)
for point in self.click_positions:
self.scene.addEllipse(point.x(), point.y(), 2, 2, pen)
self.click_positions = []


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
widget = MainWidget()
widget.resize(640, 480)
widget.show()
sys.exit(app.exec_())

QGraphicsScene有很多特点。

阅读Graphics View Framework overview在 Qt 文档中。

关于python - pyqt 在现有的 GUI 小部件上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8993842/

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