在 QMainWindow 上显示的自定义 QGraphicsView 中的 PyQT 绘图

基本思想是我不能在 QMainWindow 中使用 QGraphicsView 类进行绘制。我可以看到 painEvent 触发,并且信息正在流向执行绘图的方法,但最终没有显示任何内容。这是带有 QGraphicsView 的类:


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, 650, 650)

        #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)

        self.drawRectangles(qp)

        qp.end()



    def drawRectangles(self, qp, w = 16):

        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)*lr)

                        g = int(math.log(g)*hr)

                        b = int(math.log(b)*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


浮云间
浏览 178回答 1
1回答

人到中年有点甜

QGraphicsView 继承自 QAbstractScrollArea 所以 QPainter 必须在 中设置viewport(),即:def paintEvent(self, e):&nbsp; &nbsp; qp = QPainter()&nbsp; &nbsp; qp.begin(self.viewport())&nbsp; &nbsp; self.drawRectangles(qp)&nbsp; &nbsp; qp.end()尽管我会绘制它并不是最好的,因为 QGraphicsView 有一个使用这些项目的绘制层。在这种情况下,最好实现一个自定义项目,我也改进了你的算法。:import sysimport numpy as npimport cv2from PyQt5 import QtCore, QtGui, QtWidgetsclass OpenCVItem(QtWidgets.QGraphicsItem):&nbsp; &nbsp; def __init__(self, img, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super(OpenCVItem, self).__init__(parent)&nbsp; &nbsp; &nbsp; &nbsp; res = 40&nbsp; &nbsp; &nbsp; &nbsp; self.grid = -np.ones((res, res))&nbsp; &nbsp; &nbsp; &nbsp; self._img = img&nbsp; &nbsp; &nbsp; &nbsp; height, width, channel = self._img.shape&nbsp; &nbsp; &nbsp; &nbsp; bytesPerLine = 3 * width&nbsp; &nbsp; &nbsp; &nbsp; self._qimage = QtGui.QImage(self._img.data,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width, height,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytesPerLine,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QtGui.QImage.Format_RGB888).rgbSwapped()&nbsp; &nbsp; def boundingRect(self):&nbsp; &nbsp; &nbsp; &nbsp; w, h, _ = self._img.shape&nbsp; &nbsp; &nbsp; &nbsp; return QtCore.QRectF(0, 0, w, h)&nbsp; &nbsp; def paint(self, painter, option, widget):&nbsp; &nbsp; &nbsp; &nbsp; painter.drawImage(0, 0, self._qimage)&nbsp; &nbsp; &nbsp; &nbsp; self.drawRectangles(painter)&nbsp; &nbsp; def drawRectangles(self, painter):&nbsp; &nbsp; &nbsp; &nbsp; mode = 0&nbsp; &nbsp; &nbsp; &nbsp; lr = 20&nbsp; &nbsp; &nbsp; &nbsp; hr = 35&nbsp; &nbsp; &nbsp; &nbsp; painter.save()&nbsp; &nbsp; &nbsp; &nbsp; painter.setPen(QtGui.QPen(QtGui.QColor("#d4d4d4")))&nbsp; &nbsp; &nbsp; &nbsp; w1, h1 = self.grid.shape&nbsp; &nbsp; &nbsp; &nbsp; fw = self.boundingRect().width()/w1&nbsp; &nbsp; &nbsp; &nbsp; fh = self.boundingRect().height()/h1&nbsp; &nbsp; &nbsp; &nbsp; s = QtCore.QSizeF(fw, fh)&nbsp; &nbsp; &nbsp; &nbsp; for idx, v in np.ndenumerate(self.grid):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if v == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r_ = QtCore.QRectF(fw*QtCore.QPointF(*idx), s)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r_int = r_.toRect()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (r, g, b), _ = cv2.meanStdDev(self._img[r_int.left():r_int.right(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r_int.top():r_int.bottom()])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if mode == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = np.log(r+1)*lr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g = np.log(g+1)*hr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = np.log(b+1)*lr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif mode == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if r+50 <= 220: r = r+50&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if g+80 <= 255: g = g+80&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if b+50 <= 220: b = b+50&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if r+70 <= 220: r = r+70&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if g+140 <= 255: g = g+140&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if b+70 <= 220: b = b+70&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; painter.setBrush(QtGui.QColor(*(int(x) for x in (r, g, b))))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; painter.drawRect(r_)&nbsp; &nbsp; &nbsp; &nbsp; painter.restore()&nbsp; &nbsp; def mousePressEvent(self, event):&nbsp; &nbsp; &nbsp; &nbsp; w1, h1 = self.grid.shape&nbsp; &nbsp; &nbsp; &nbsp; fw = self.boundingRect().width()/w1&nbsp; &nbsp; &nbsp; &nbsp; fh = self.boundingRect().height()/h1&nbsp; &nbsp; &nbsp; &nbsp; xi = int(event.pos().x()/fw)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; yi = int(event.pos().y()/fh)&nbsp; &nbsp; &nbsp; &nbsp; self.grid[xi][yi] = -self.grid[xi][yi]&nbsp; &nbsp; &nbsp; &nbsp; self.update()&nbsp; &nbsp; &nbsp; &nbsp; super(OpenCVItem, self).mousePressEvent(event)class Display_Pixels(QtWidgets.QGraphicsView):&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super(Display_Pixels, self).__init__(parent)&nbsp; &nbsp; &nbsp; &nbsp; scene = QtWidgets.QGraphicsScene(self)&nbsp; &nbsp; &nbsp; &nbsp; self.setScene(scene)&nbsp; &nbsp; &nbsp; &nbsp; item = OpenCVItem(cv2.imread("roi.jpg"))&nbsp; &nbsp; &nbsp; &nbsp; scene.addItem(item)class Window(QtWidgets.QMainWindow):&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super(Window,self).__init__(parent=parent)&nbsp; &nbsp; &nbsp; &nbsp; self.setGeometry(1,31,900,900)&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle("Pre-Alignment system")def run():&nbsp; &nbsp; app = QtWidgets.QApplication.instance()&nbsp; &nbsp; if app is None:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; app = QtWidgets.QApplication(sys.argv)&nbsp; &nbsp; GUI = Window()&nbsp; &nbsp; view = Display_Pixels(GUI)&nbsp; &nbsp; GUI.setCentralWidget(view)&nbsp; &nbsp; GUI.show()&nbsp; &nbsp; sys.exit(app.exec_())run()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python