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.
Thank you so much!!!
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_())
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…