pyqt在现有的GUI小部件上绘图 [英] pyqt drawing on an exsiting widget of GUI

查看:94
本文介绍了pyqt在现有的GUI小部件上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to pyqt. I am doing a program that allows you clicks on the picture and remember the coordinates of points you clicks and draw a stickfigure on a widget of the GUI. My code right now can prompt out a new window to show a polygon with 4 points. However, I hope it can be displayed on the ui file I alreay made by pyqt. The object name for the widget is called widget.I hope someone can help me to modify the code to display the polygon on the gui widget not prompting out a new window.

非常感谢!!!

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 上?在这种情况下,您可以向场景中添加项目:

Looks like you want to draw items on QGraphicsScene? In this case you could add items to the scene:

#!/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 有很多特征。

QGraphicsScene has many features.

阅读图形视图框架概述在Qt文档中。

这篇关于pyqt在现有的GUI小部件上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆