使用drawBackground在QGraphicsView上绘画背景 [英] Painting background on QGraphicsView using drawBackground

查看:1606
本文介绍了使用drawBackground在QGraphicsView上绘画背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试绘制和绘制QGraphicsView/Scene时遇到问题.我正在绘制一堆QLineF as background overriding QGraphicsView :: drawBackGround`.但是,当我尝试更改背景颜色时,没有任何反应.

I have a problem trying to paint and draw on QGraphicsView/Scene. I'm drawing a bunch of QLineF as background overridingQGraphicsView::drawBackGround`. However, when I try to change the background color nothing happens.

以下是我正在做的事情的最小示例:

Here is minimal example of what I'm doing:

import sys
import platform
import ctypes
from PySide import QtCore, QtGui
from mygv import Ui_Dialog
import sys

class myView(QtGui.QDialog):
    def __init__(self, parent = None):
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.view.drawBackground = self.drawBackground
        self.ui.view.wheelEvent = self.wheelEvent
        self.scene = QtGui.QGraphicsScene()
        self.ui.view.setScene(self.scene)
        self.scene.addEllipse(0,0,100,100)

    def drawBackground(self, painter, rect):

        bbrush = QtGui.QBrush( QtGui.QColor(255,170,255), QtCore.Qt.SolidPattern)
        painter.setBackgroundMode(QtCore.Qt.OpaqueMode)

        pen = QtGui.QPen(QtGui.QColor(46, 84, 255))
        pen.setWidth(5)
        painter.setPen(pen)

        line1 = QtCore.QLineF(0,0,0,100)
        line2 = QtCore.QLineF(0,100,100,100)
        line3 = QtCore.QLineF(100,100,100,0)
        line4 = QtCore.QLineF(100,0,0,0)
        painter.setBackground(bbrush)
        painter.drawLines([line1, line2, line3, line4])




    def wheelEvent(self,event):
        factor = 1.41 ** (event.delta() / 240.0)
        self.ui.view.scale(factor, factor)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    diag = myView()
    diag.show()
    diag.ui.view.centerOn(50,50)
    app.exec_()

Ui_dialog只是从QDesigner生成的一个标准对话框,带有一个名为"view"的QGraphicsView成员.

Ui_dialog is just a standard dialog generated from QDesigner with a QGraphicsView member named "view".

这只是问题的一个例子.我需要能够在执行应用程序期间系统地更改背景的颜色.

This just an example of the problem. I need to be able to change the color of the background systematically during the execution of my app.

我在想什么或做的(显然)是错的?

What am I missing or doing (clearly) wrong?

推荐答案

QPaintersetBackground方法不填充背景,仅指定诸如绘制不透明文本,点画线和位图之类的操作的背景(请参见文档).

The setBackground method of the QPainter does not fill the background but only specifies the background for operations like drawing opaque text, stippled lines and bitmaps (see the documentation).

您可以使用fillRect代替,先用指定的画笔填充可绘制区域大小的矩形.

You can use fillRect instead to first fill a rectangle of the size of your paintable area with the brush you specified.

示例:

import sys
from PyQt5 import QtCore, QtWidgets, QtGui

class myView(QtWidgets.QGraphicsView):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def drawBackground(self, painter, rect):

        background_brush = QtGui.QBrush( QtGui.QColor(255,170,255), QtCore.Qt.SolidPattern)
        painter.fillRect(rect, background_brush)

        pen = QtGui.QPen(QtGui.QColor(46, 84, 255))
        pen.setWidth(5)
        painter.setPen(pen)

        line1 = QtCore.QLineF(0,0,0,100)
        line2 = QtCore.QLineF(0,100,100,100)
        line3 = QtCore.QLineF(100,100,100,0)
        line4 = QtCore.QLineF(100,0,0,0)
        painter.drawLines([line1, line2, line3, line4])

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    scene = QtWidgets.QGraphicsScene()
    scene.addEllipse(0,0,100,100)

    view = myView(scene)
    view.show()
    view.centerOn(50,50)

    app.exec_()

它使用PyQt5,但相当容易理解.

It uses PyQt5 but is fairly straightforward to understand.

结果:

显示您指定的漂亮的品红色.

shows the nice magenta color you specified.

这篇关于使用drawBackground在QGraphicsView上绘画背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆