【问题标题】:Painting background on QGraphicsView using drawBackground使用 drawBackground 在 QGraphicsView 上绘制背景
【发布时间】:2017-04-23 05:17:09
【问题描述】:

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

这是我正在做的最小示例:

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 成员。

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

我错过了什么或做错了什么(显然)?

【问题讨论】:

    标签: qt background pyqt pyside qgraphicsview


    【解决方案1】:

    QPaintersetBackground 方法不填充背景,而只为绘制不透明文本、点画线和位图等操作指定背景(参见documentation)。

    您可以改用fillRect,先用您指定的画笔填充一个与可绘制区域大小相同的矩形。

    例子:

    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,但相当容易理解。

    结果:

    显示您指定的漂亮洋红色。

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2023-03-10
      • 2015-08-07
      • 1970-01-01
      相关资源
      最近更新 更多