在用QT Designer制造的QT表单中,获取QT小部件以更新鼠标事件



我做了一个自定义的qgraphicsview,我将其添加为窗口小部件,并在我使用qt Designer制作的应用程序表单中。一切似乎都在起作用,包括鼠标点击。使用图纸的方法被调用,但是我很难在屏幕上实际重新粉刷。我尝试创建PaintEvent方法的副本,并以Mouseevent方法调用。我还尝试直接打电话给油漆。我似乎不能让它重新涂上Mouseevent。这是代码:

import sys
from PySide2 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QPushButton, QLineEdit, QAction, QSlider
from PySide2.QtWidgets import QListWidget, QTabWidget, QGraphicsView, QGraphicsScene
from PySide2.QtWidgets import QSpinBox, QWidget, QDialog, QVBoxLayout
from PySide2.QtGui import QPixmap, QImage, QMatrix, QPainter, QColor
from PySide2.QtGui import QMouseEvent, QCursor, QPaintEvent
from PySide2.QtCore import QFile, QObject, SIGNAL
import cv2
import numpy as np
import math
class Display_Pixels(QGraphicsView):
    def __init__(self, parent=None):
        QGraphicsView.__init__(self, parent=parent)
        #super().__init__()
        self.initUI()
        self.img = cv2.imread('roi.jpg')
    def initUI(self):      
        #self.setGeometry(100, 100, 450, 450)
        #self.setWindowTitle('By Pixel')
        #self.setMouseTracking(True)
        #self.show()
        res = 40 
        self.grid = np.array([ [-1] * res  for n in range(res)]) # list comprehension
        #print(self.grid.shape)

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self.viewport())
        self.drawRectangles(qp)
        qp.end()
    def mousePaintEvent(self):
        qp = QPainter()
        qp.begin(self.viewport())
        self.drawRectangles(qp)
        qp.end()
    def drawRectangles(self, qp, w = 20):
        print("Drawing")
        mode = 0
        x,y = 0,0 # starting position
        lr = 20
        hr = 35
        col = QColor(0, 0, 0)
        col.setNamedColor('#d4d4d4')
        qp.setPen(col)
        #print(self.img.shape)
        for g_row, img_row in zip(self.grid, self.img):
            #print(img_row.shape)
            for g_col, img_col in zip(g_row, img_row):
                r, g, b = (img_col[0], img_col[1], img_col[2])
                #print(r,g,b)
                if g_col == 1:
                    if mode == 0:
                        r = int(math.log(r+1)*lr)
                        g = int(math.log(g+1)*hr)
                        b = int(math.log(b+1)*lr)
                    elif mode == 1:
                        if r+50 <= 220: r = r+50
                        if g+80 <= 255: g = g+80
                        if b+50 <= 220: b = b+50
                    else:
                        if r+70 <= 220: r = r+70
                        if g+140 <= 255: g = g+140
                        if b+70 <= 220: b = b+70
                    qp.setBrush(QColor(r, g, b))
                    qp.drawRect(x, y, w, w)
                else:
                    qp.setBrush(QColor(r, g, b))
                    qp.drawRect(x, y, w, w)
                #qp.setBrush(QColor(200, 0, 0))
                #qp.drawRect(x, y, w, w)
                x = x + w  # move right
            y = y + w # move down
            x = 0 # rest to left edge

    def mousePressEvent(self, QMouseEvent):
        w = 16.0
        #print("MOUSE:")
        #print('(', int(QMouseEvent.x()/w), ', ', int(QMouseEvent.y()/w), ')')
        #print (QMouseEvent.pos())
        x = float(QMouseEvent.x())
        y = float(QMouseEvent.y())
        self.grid[int(y/w)][int(x/w)] = -1 * self.grid[int(y/w)][int(x/w)]
        #print(img[int(y/w), int(x/w), :])
        self.paintEvent(QPaintEvent)
        #self.mousePaintEvent()
        self.update()
        self.repaint()      
if __name__ == '__main__':
    app = QApplication.instance()
    if app is None: 
        app = QApplication(sys.argv)
    px = Display_Pixels()
    px.show()
    sys.exit(app.exec_())

您不应直接调用paintEvent方法,创建具有类似名称的方法不会神奇地调用。您必须调用viewport()update()方法。

def mousePressEvent(self, event):
    w = 16.0
    x = int(event.x()*1.0/w)
    y = int(event.y()*1.0/w)
    s1, s2 = self.grid.shape
    # verify
    if 0 <= y < s1 and 0 <= x < s2:
        self.grid[x][y] = -self.grid[x][y]
        self.viewport().update()  

相关内容

  • 没有找到相关文章

最新更新