获取 Qt 小部件以更新使用 Qt Designer 制作的 Qt 表单内的鼠标事件
Getting a Qt Widget to update a mouse event inside a Qt form made with Qt Designer
我制作了一个自定义 QGraphicsView,我将其作为小部件添加到我使用 Qt Designer 制作的应用程序表单中。一切似乎都在工作,包括鼠标点击。执行绘图的方法被调用,但我无法在屏幕上实际重新绘制。我尝试创建 paintEvent 方法的副本并调用 mouseEvent 方法。我也试过直接调用 paintEvent。我似乎无法使用 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()
我制作了一个自定义 QGraphicsView,我将其作为小部件添加到我使用 Qt Designer 制作的应用程序表单中。一切似乎都在工作,包括鼠标点击。执行绘图的方法被调用,但我无法在屏幕上实际重新绘制。我尝试创建 paintEvent 方法的副本并调用 mouseEvent 方法。我也试过直接调用 paintEvent。我似乎无法使用 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()