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

时间:2019-02-14 16:30:27

标签: python pyqt qgraphicsview

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

<table class="table table-bordered table-hover table-stripped">
                <thead>
                <tr>
                    <th>No</th>
                    <th>Operaci&oacute;n</th>
                    <th>Tupla</th>
                    <th>Tabla</th>
                    <th>Valores antiguos</th>
                    <th>Valores Actuales</th>
                    <th>URL</th>
                    <th>IP</th>
                    <th>Creado</th>
                    <th>Actualizado</th>
                </tr>
                </thead>
                <tbody>
                <?php $no = 1 ?>
                @foreach($audit as $item)
                    <tr>
                        <td>{{ $no++ }}</td>
                        <td>{{ $item->event }}</td>
                        <td>{{ $item->auditable_id }}</td>
                        <td>{{ $item->auditable_type }}</td>
                        <td>{{ $item->old_values }}</td>
                        <td>{{ $item->new_values }}</td>
                        <td>{{ $item->url }}</td>
                        <td>{{ $item->ip_address }}</td>
                        <td>{{ $item->created_at }}</td>
                        <td>{{ $item->updated_at }}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>

还有主窗口的代码:

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

                    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.repaint()
        #self.update()

1 个答案:

答案 0 :(得分:2)

QGraphicsView继承自QAbstractScrollArea,因此QPainter必须在viewport()中进行设置,即:

def paintEvent(self, e):
    qp = QPainter()
    qp.begin(self.viewport())
    self.drawRectangles(qp)
    qp.end()

尽管我会画画不是最好的,因为QGraphicsView有一个使用项目的画图层。在这种情况下,最好实现一个自定义项,我也改进了您的算法。

import sys
import numpy as np
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets

class OpenCVItem(QtWidgets.QGraphicsItem):
    def __init__(self, img, parent=None):
        super(OpenCVItem, self).__init__(parent)
        res = 40
        self.grid = -np.ones((res, res))
        self._img = img
        height, width, channel = self._img.shape
        bytesPerLine = 3 * width
        self._qimage = QtGui.QImage(self._img.data, 
            width, height, 
            bytesPerLine, 
            QtGui.QImage.Format_RGB888).rgbSwapped()

    def boundingRect(self):
        w, h, _ = self._img.shape
        return QtCore.QRectF(0, 0, w, h)

    def paint(self, painter, option, widget):
        painter.drawImage(0, 0, self._qimage)
        self.drawRectangles(painter)

    def drawRectangles(self, painter):
        mode = 0
        lr = 20
        hr = 35
        painter.save()
        painter.setPen(QtGui.QPen(QtGui.QColor("#d4d4d4")))
        w1, h1 = self.grid.shape
        fw = self.boundingRect().width()/w1
        fh = self.boundingRect().height()/h1
        s = QtCore.QSizeF(fw, fh)
        for idx, v in np.ndenumerate(self.grid):            
            if v == 1:
                r_ = QtCore.QRectF(fw*QtCore.QPointF(*idx), s)
                r_int = r_.toRect()
                (r, g, b), _ = cv2.meanStdDev(self._img[r_int.left():r_int.right(), 
                    r_int.top():r_int.bottom()])
                if mode == 0:
                    r = np.log(r+1)*lr
                    g = np.log(g+1)*hr
                    b = np.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
                painter.setBrush(QtGui.QColor(*(int(x) for x in (r, g, b))))
                painter.drawRect(r_)
        painter.restore()

    def mousePressEvent(self, event):
        w1, h1 = self.grid.shape
        fw = self.boundingRect().width()/w1
        fh = self.boundingRect().height()/h1
        xi = int(event.pos().x()/fw) 
        yi = int(event.pos().y()/fh)
        self.grid[xi][yi] = -self.grid[xi][yi]
        self.update()
        super(OpenCVItem, self).mousePressEvent(event)

class Display_Pixels(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(Display_Pixels, self).__init__(parent)
        scene = QtWidgets.QGraphicsScene(self)
        self.setScene(scene)
        item = OpenCVItem(cv2.imread("roi.jpg"))
        scene.addItem(item)

class Window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Window,self).__init__(parent=parent)
        self.setGeometry(1,31,900,900)
        self.setWindowTitle("Pre-Alignment system")

def run():
    app = QtWidgets.QApplication.instance()
    if app is None: 
        app = QtWidgets.QApplication(sys.argv)
    GUI = Window()
    view = Display_Pixels(GUI)
    GUI.setCentralWidget(view)
    GUI.show()
    sys.exit(app.exec_())

run()