Update the Opacity of QGraphicsItem

S.H.

I want to update the opacity of some QGraphicsItem after the mouse clicking. As suggested from other solution, the QGraphicScene manually update the GraphicsItem after the mouser press event. I have tried different setOpacity() and update() in QGraphicsScene and QGraphicsItem. But none works and do not know what is wrong.

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

CUBE_POS = {
    "a":(   8.281,  18.890),
    "b":(   8.668,  23.692),
    "c":(   21.493, 23.423),
    "d":(   21.24,  15.955),
    }        

class CubeItem(QGraphicsItem):

    def __init__(self, x, y, parent=None):
        super(CubeItem,self).__init__(parent)
        self.x = x
        self.y = y
        self.polygon = QPolygonF([
            QPointF(self.x-10, self.y-10), QPointF(self.x-10, self.y+10),
            QPointF(self.x+10, self.y+10), QPointF(self.x+10, self.y-10),
            ])
        self._painter = QPainter()

    ##Estimate the drawing area
    def boundingRect(self):
        return QRectF(self.x-10, self.y-10, 20, 20)

    ##Real Shape of drawing area
    def shape(self):
        path = QPainterPath()
        path.addRect(self.x-10, self.y-10, 20, 20)
        return path

    ##paint function called by graphicview
    def paint(self, painter, option, widget):
        painter.setBrush(Qt.red)
        painter.setOpacity(0.2)
        painter.drawRect(self.x-10, self.y-10, 20, 20)
        self._painter = painter

    def activate(self):
        try:
            #self._painter.setOpacity(1.0)
            self.setOpacity(1.0)
            self.update()
        except ValueError as e:
            print(e)

class TagScene(QGraphicsScene):

    def __init__(self, parent=None):
        super(TagScene, self).__init__(parent)

        self.cubes_items_ref = {}
        self.addCubes()

    def addCubes(self):
        for cube in CUBE_POS:
            newCube = CubeItem(CUBE_POS[cube][0]*15, 
                                   CUBE_POS[cube][1]*15)
            self.addItem(newCube)
            self.cubes_items_ref[cube] = newCube

    def mousePressEvent(self, event):
        print("mouse pressed")

        #for cube in self.cubes_items_ref:
        #    self.cubes_items_ref[cube].setOpacity(1.0)
        #    #self.cubes_items_ref[cube].activate()
        #self.update(QRectF(0,0,500,500))

        for cube in self.items():
            cube.setOpacity(1.0)
        self.update(QRectF(0,0,500,500))

class MainWindow(QMainWindow):

     def __init__(self):
        super(MainWindow, self).__init__()
        layout = QHBoxLayout()
        self.scene = TagScene()
        self.view = QGraphicsView(self.scene)
        self.scene.setSceneRect(QRectF(0,0,500,500))
        layout.addWidget(self.view)
        self.widget = QWidget()
        self.widget.setLayout(layout)
        self.setCentralWidget(self.widget)

if __name__ == "__main__":

    app = QApplication(sys.argv)
    test = MainWindow()
    test.show()
    sys.exit(app.exec_())
eyllanesc

The problem is that when you overwrite the paint method of the QGraphicsItem you are setting a constant opacity

def paint(self, painter, option, widget):
    painter.setBrush(Qt.red)
    painter.setOpacity(0.2) # <-- this line is the problem
    painter.drawRect(self.x-10, self.y-10, 20, 20)
    self._painter = painter

And you will not use the opacity that the QPainter already passes paint() method.

If you want to set an initial opacity you must do it in the constructor.On the other hand the setOpacity() method already calls update() so it is not necessary to make an explicit call.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

CUBE_POS = {
    "a": (8.281, 18.890),
    "b": (8.668, 23.692),
    "c": (21.493, 23.423),
    "d": (21.24, 15.955),
}


class CubeItem(QtWidgets.QGraphicsItem):
    def __init__(self, x, y, parent=None):
        super(CubeItem, self).__init__(parent)
        self.x = x
        self.y = y
        self.polygon = QtGui.QPolygonF(
            [
                QtCore.QPointF(self.x - 10, self.y - 10),
                QtCore.QPointF(self.x - 10, self.y + 10),
                QtCore.QPointF(self.x + 10, self.y + 10),
                QtCore.QPointF(self.x + 10, self.y - 10),
            ]
        )
        self.setOpacity(0.2) # initial opacity

    ##Estimate the drawing area
    def boundingRect(self):
        return QtCore.QRectF(self.x - 10, self.y - 10, 20, 20)

    ##Real Shape of drawing area
    def shape(self):
        path = QtGui.QPainterPath()
        path.addRect(self.boundingRect())
        return path

    ##paint function called by graphicview
    def paint(self, painter, option, widget):
        painter.setBrush(QtCore.Qt.red)
        painter.drawRect(self.x - 10, self.y - 10, 20, 20)


class TagScene(QtWidgets.QGraphicsScene):
    def __init__(self, parent=None):
        super(TagScene, self).__init__(parent)

        self.cubes_items_ref = {}
        self.addCubes()

    def addCubes(self):
        for cube in CUBE_POS:
            newCube = CubeItem(CUBE_POS[cube][0] * 15, CUBE_POS[cube][1] * 15)
            self.addItem(newCube)
            self.cubes_items_ref[cube] = newCube

    def mousePressEvent(self, event):
        for cube in self.items():
            cube.setOpacity(1.0) # update opacity


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        layout = QtWidgets.QHBoxLayout()
        self.scene = TagScene()
        self.view = QtWidgets.QGraphicsView(self.scene)
        self.scene.setSceneRect(QtCore.QRectF(0, 0, 500, 500))
        layout.addWidget(self.view)
        self.widget = QtWidgets.QWidget()
        self.widget.setLayout(layout)
        self.setCentralWidget(self.widget)


if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    test = MainWindow()
    test.show()
    sys.exit(app.exec_())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive