如何与Pyqt5中的主窗口和添加的小部件进行交互?

时间:2019-05-28 06:33:19

标签: python pyqt pyqt5

我正在制作的Gui如下。

  1. 通过open按钮下载照片
  2. 用鼠标检查图像以在图片上绘画
  3. 计算每次使用鼠标检查图像时通过该功能单击鼠标的次数

我可以完成#1和#2。

但是问题是#3

我引用了the link
在此剪贴板上放置和编辑想要的图片没有问题
当mainWindow和鼠标控件功能发生冲突时,会出现问题。

如果您回答

,我将不胜感激

MainWindow鼠标控件

    @pyqtSlot()  # prevents executing following function twice
    def mouseButtonKind(self, buttons):
        itemT = int(self.tw_current.item(0, 0).text())
        itemV = int(self.tw_current.item(1, 0).text())
        if buttons & Qt.LeftButton:
            print('LEFT')
            itemT = itemT+1

        if buttons & Qt.RightButton: 
            print('RIGHT')
            itemV = itemV+1

        self.tw_current.item(0, 0).setText(str(itemT))
        self.tw_current.item(0, 1).setText(str(itemT*100/49))
        self.tw_current.item(1, 0).setText(str(itemV))
        self.tw_current.item(1, 1).setText(str(itemV*100/49))


    def mousePressEvent(self, e):  # e ; QMouseEvent
        print('(%d %d)' % (e.x(), e.y()))
        self.mouseButtonKind(e.buttons())

    def btn_openimg_clicked(self):
        drawer = Drawer()
        self.layout_imgView.addWidget(drawer)
        self.btnSave = QPushButton("Save image")
        self.btnClear = QPushButton("Clear")
        self.btnSave.clicked.connect(drawer.saveImage)
        self.btnClear.clicked.connect(drawer.clearImage)
        self.layout_imgView.addWidget(self.btnSave)
        self.layout_imgView.addWidget(self.btnClear)
        self.layout_imgView.addWidget(drawer)

小部件抽屉代码

class Drawer(QWidget):
    newPoint = pyqtSignal(QPoint)

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setAttribute(QtCore.Qt.WA_StaticContents)
        self.modified = False
        self.scribbling = False
        imageSize = QtCore.QSize(9500, 9500)
        h=400
        w=400
        self.image = QtGui.QImage()
        self.image=QtGui.QImage(w,h,QtGui.QImage.Format_RGB32)
        self.path = QPainterPath()   


    def setPenColor(self, newColor):
        self.myPenColor = newColor

    def setPenWidth(self, newWidth):
        self.myPenWidth = newWidth

    def clearImage(self):

        self.image.fill(QtGui.qRgb(255, 255, 255))  ## switch it to else 
        self.modified = True
        self.update()


    def paintEvent(self, event):
        painter = QPainter(self)

        painter.setPen(QColor(0, 0, 0))

        painter.setFont(QFont('Decorative', 10))
        painter.drawImage(event.rect(), self.image)
        painter.drawPath(self.path)

    def mousePressEvent(self, event):
        self.path.moveTo(event.pos())
        self.update()

    def mouseMoveEvent(self, event):
        self.path.lineTo(event.pos())
        self.newPoint.emit(event.pos())
        self.update()

    def sizeHint(self):
        return QSize(200, 200)

0 个答案:

没有答案