Thursday 5 September 2019

How to set window and it's graphicScene transparent(using slider) and leave only QPushButton visible

I want my window and it's QGraphicsScene to be transparent which is controlled by QSlider.

I tried looking for some answers here and there, but it's mainly setting background color to 0 :

self.setStyleSheet("background-color:rgba(0, 0, 0, 0);")

or like that :

self.setWindowFlags(Qt.FramelessWindowHint) 
self.setAttribute(Qt.WA_TranslucentBackground)

Which does it job, but I would like to make slider which will control level of transparency and I don't see how can I do it with commands above.

Can somebody please give me some advise how to do it ? Thank you

I will attach my test code here below(I want to be able to control transparency of everything , but do not touch slider and button):

from PySide2.QtGui import QBrush, QColor
from PySide2.QtCore import QSize, Qt
from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QFrame, QSizePolicy, QApplication, QSlider, QPushButton

class MainWindow(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.mainLayout = QVBoxLayout()
        self.graphicsWidget = MainGraphicsWidget()
        self.window = 'transp_test'
        self.title = 'transparent UI'
        self.size = (1000, 650)
        self.create()


    def create(self, **kwargs):
        self.setWindowTitle(self.title)
        self.resize(QSize(*self.size))
        self.setLayout(self.mainLayout)
        self.mainLayout.addWidget(self.graphicsWidget)


class MainGraphicsWidget(QGraphicsView):
    def __init__(self, parent=None):
        super(MainGraphicsWidget, self).__init__(parent)
        self._scene = QGraphicsScene(backgroundBrush = Qt.gray)
        self.setScene(self._scene)

        self.transpSlider = QSlider()
        self.transpSlider.setRange(0,100)

        self.mainButton = QPushButton('I want it to be "Test" button')

        self._scene.addWidget(self.mainButton)
        self._scene.addWidget(self.transpSlider)
        self.transpSlider.move(300, 100)

        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
        self.setFrameShape(QFrame.NoFrame)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = MainWindow()
    window.setGeometry(600, 300, 600, 600)
    window.show()
    sys.exit(app.exec_())



from How to set window and it's graphicScene transparent(using slider) and leave only QPushButton visible

No comments:

Post a Comment