1
필자는 PyQt에 익숙하다. 사용자가 마우스 (숫자 인식 시스템)를 사용하여 숫자를 그릴 수있는 기능을 구현해야하는 프로젝트에서 작업 중이다. 그래서 내가 원하는 것은 마우스 버튼을 누르면 버튼을 놓을 때까지 앱이 그리기 시작합니다. 이 소스 코드를 만들었지 만 여전히 작동하지 않습니다 (나는 PaintEvent()
에 신호를 보내는 데 어려움을 겪고 있다고 생각합니다).PyQt5 : 이벤트를 사용하여 페인팅
import sys
from PyQt5 import QtCore
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog,QGraphicsView,QGraphicsScene,QVBoxLayout
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication
class Communicate(QObject):
drawApp = pyqtSignal()
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Simple')
self.setMouseTracking(True)
self.label = QLabel(self)
self.label.resize(500, 40)
self.c = Communicate()
self.c.drawApp.connect(self.PaintEvent())
self.show()
def mousePressEvent(self, event):
self.c.drawApp.emit()
self.startingposx = event.x()
self.startingposy = event.y()
#super().mousePressEvent(event)
print ("mouse pressed")
def mouseMoveEvent(self, event):
self.label.setText('Coordinates: (%d : %d)' % (event.x(), event.y()))
self.y = event.y()
self.x=event.x()
def PaintEvent(self,event):
qp = QPainter()
qp.begin(self)
#qp.setPen(Qt.red)
qp.drawPoints(self,qp)
qp.end()
self.update()
def mouseReleaseEvent(self,event):
self.endingposx = event.x()
self.endingposy = event.y()
super().mouseReleaseEvent(event)
print("starting point was",self.startingposx)
print("starting point y was ",self.startingposy)
print("ending point was ",self.endingposx)
print("ending point was y ",self.endingposy)
print("released")
def drawPoints(self,qp):
qp.setPen(Qt.red)
size = self.size()
x=self.x
y=self.y
qp.drawPoint(x,y)
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
app.exec_()
eyllanesc 내가 시도 QPainting 객체 의 배경을 설정하는 방법을 인간하시기 바랍니다 :하지만 데프 paintEvent (자체 이벤트) 작동하지 않습니다 : 화가 (QFont ('장식', 10)) bbrush = QtGui.QBrush (QtGui.QColor (0,0,0)) QPainter (자체) painter.setPen (QColor (168, 34, 3)) painter.setFont ,(Painter, bbrush) painter.drawPath (self.path) – hbdz