Can't set transparency of QWindow after shown once while opaque - python

I have a window I'm trying to make transparent after being shown once while opaque. However, doesn't go transparent even if I hide and show again. This is what I get:
But I want it like this: (transparent against background)
If I run WA_TranslucentBackground first, before .show() it creates a nice transparent button as expected.
Here is an example code which shows a floating button when you run it
import sys
from PySide2 import QtCore, QtWidgets, QtGui
class TransparentWindow(QtWidgets.QMainWindow):
def __init__(self):
super(TransparentWindow, self).__init__()
self.setCentralWidget(QtWidgets.QWidget(self))
self.mainLayout = QtWidgets.QVBoxLayout()
self.centralWidget().setLayout(self.mainLayout)
self.mainLayout.addWidget(QtWidgets.QPushButton("Hello World", parent=self))
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.show() # If I move this under '.setAttribute()' it works
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
demo = TransparentWindow()
sys.exit(app.exec_())
I've tried hiding and reshowing, 'setAutoFillBackground()' and 'WA_NoSystemBackground' attribute, but I cant seem to get it to work. Anybody have any idea how to set the transparency after it has been shown as opaque?
Using versions QtCore: 5.9.6, Pyside2: 5.9.0a1.dev1528389443

Related

How to give glass effect to the background in pyqt5 without background image

I have made an application using pyqt5 and I want to give a glass effect in it's background without background image. How to do this?
I guess you want this ==> setWindowOpacity
from PyQt5.QtWidgets import *
class main(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowOpacity(0.5)
self.resize(320,250)
self.show()
app = QApplication([])
window = main()
app.exec()

PyQt resize QMovie with proper anti-aliasing

I created a QLabel and set it's movie to a QMovie object with an animated gif. Then in the resizeEvent of my app I resize and move the label, so that it is centered/fit to the layout. This works fine but the movie has a lot of fine lines which get totally garbled in the resize operation, it seems there is no anti-aliasing. So either I am using the wrong resize method, or I need to set anti-aliasing somewhere right? There is nothing in the QMovie or QLabel documentation to suggest how to do this. I did read that the QMovie is inherited from a QImageReader, though that too has no property for anti-aliasing that I could find.
EDIT
I did somewhat get this to work, but it's still not quite right. I found that QMovie has a setScaledSize method which actually scales the underlying QImageViewer. Then I just have the label adjust to it's contents, namely the movie. Using the following code I am able to resize the movie with proper anti-aliasing, however it is quite "jumpy" and "flickery" during resize so obviously I'm not doing this quite "right". Also it somehow loses it's aspect ratio sometimes. Still looking for the correct way to do this... Maybe a QLabel is the wrong way to go?
Here's a working example
import sys
from PyQt4 import QtGui
class MovieTest(QtGui.QDialog):
def __init__(self):
super(MovieTest, self).__init__()
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
self.loading_lbl = QtGui.QLabel()
self.loading_lbl.setSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicyIgnored)
self.loading_lbl.setScaledContents(True)
layout.addWidget(self.loading_lbl)
loading_movie = QtGui.QMovie("loading-radial_loop.gif") # some gif in here
self.loading_lbl.setMovie(loading_movie)
loading_movie.start()
self.setGeometry(50,50,100,100)
self.setMinimumSize(10,10)
def resizeEvent(self, event):
rect = self.geometry()
size = min(rect.width(), rect.height())
movie = self.loading_lbl.movie()
movie.setScaledSize(QtCore.QSize(size, size))
self.loading_lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = MovieTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Ok I have this figured out now, with just a few tweaks to the code in my edited post. The secret is in keeping the label the full size of it's parent rect (in this case the size of the whole layout) and then scaling the movie within the label. Essentially you are internally scaling the movie, instead of having it automatically fill the contents of the label. From what I can tell this changes around the order of operations a bit and allows the movie to scale itself on render, instead of rendering the frame and then scaling it to the label size.
Working code:
import sys
from PyQt4 import QtGui, QtCore
class MovieTest(QtGui.QDialog):
def __init__(self):
super(MovieTest, self).__init__()
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
self.loading_lbl = QtGui.QLabel()
self.loading_lbl.setStyleSheet('border: 1px solid red') # just for illustration
self.loading_lbl.setAlignment(QtCore.Qt.AlignCenter)
layout.addWidget(self.loading_lbl)
loading_movie = QtGui.QMovie("loading-radial_loop.gif")
self.loading_lbl.setMovie(loading_movie)
loading_movie.start()
self.setGeometry(50,50,100,100)
self.setMinimumSize(10,10)
def resizeEvent(self, event):
rect = self.geometry()
size = QtCore.QSize(min(rect.width(), rect.height()), min(rect.width(), rect.height()))
movie = self.loading_lbl.movie()
movie.setScaledSize(size)
def main():
app = QtGui.QApplication(sys.argv)
ex = MovieTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

PyQt4 Label not showing

I am very new to PyQt4 and this question is probably very simple but I have tried many different things and nothing works. I am trying to make a label in PyQt4.
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class Display(QtGui.QWidget):
def __init__(self):
super(Display, self).__init__()
self.time = Time() #Another class in the program
self.ShowFullScreen()
self.setStyleSheet("background-color: black;")
self.show()
self.MainDisplay()
def MainDisplay(self):
self.timedisplay = QtGui.QLabel(self)
self.timedisplay.setStyleSheet("font: 30pt Helvetica; color: white;")
self.timedisplay.setText(time.GetTime())
self.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
GUI = Display()
sys.exit(app.exec())
The label doesn't show up, and there is no error message. What am I doing wrong?
I use PySide and not Qt, but they are supposed to be 99.99% compatible. The main problem is your call to the show() function, which makes the window visible. You have two calls to show. The first time it is called, you have not yet called MainDisplay so the QLabel hasn't been created yet. The second time you call show the window is already visible, so nothing changes.
If you create the widgets first and call show once, at the end, it will work the way you want. With this code the label shows up.
There are other issues:
You will have to change the import statements back the way you had them.
I did not have your Time class, so I just wrote a simple piece of text into the label.
The function ShowFullScreen should be showFullScreen.
The function that runs the event loop in QtApp is named exec_ not exec.
import sys
from PySide import QtCore
from PySide import QtGui
class Display(QtGui.QWidget):
def __init__(self):
super(Display, self).__init__()
self.setStyleSheet("background-color: black;")
self.MainDisplay()
self.showFullScreen()
def MainDisplay(self):
self.timedisplay = QtGui.QLabel(self)
self.timedisplay.setStyleSheet("font: 30pt Helvetica; color: white;")
self.timedisplay.setText("What time is it now?")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
GUI = Display()
sys.exit(app.exec_())

How to change the background color in PyQt4

I am really new to PyQt4(just learned what it is two days ago) and I know that this question probably has a really easy answer but I can't find it anywhere. I am trying to change the background color of the window to black. I have this code so far.
import sys
from PyQt4 import QtGui
class Display(QtGui.QMainWindow):
def __init__(self):
super(Display, self).__init__()
self.ShowFullScreen()
self.show()
if name == '__main__':
app = QtGui.QApplication(sys.argv)
GUI = Display()
sys.exit(app.exec())
When I run this I get a white window. Is there a method I could run to change it to black?
You can set styleSheet for window.
self.showFullScreen()
self.setStyleSheet("background-color: black;")
Reference
Stylesheet Reference

QDockWidgets and QLayouts

I've just came across QDockWidgets. And I am blown away with the flexibility these widgets offer. But it appears they do require a proper planning ahead.
I put a simple example here:
import sys
from PyQt4 import QtCore, QtGui
class GUI(QtGui.QMainWindow):
def __init__(self):
super(GUI, self).__init__()
mainWidget=QtGui.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtGui.QVBoxLayout()
mainWidget.setLayout(mainLayout)
DockA = QtGui.QDockWidget('DockA')
DockB = QtGui.QDockWidget('DockB')
mainLayout.addWidget(DockA)
mainLayout.addWidget(DockB)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
dialog = GUI()
dialog.show()
dialog.raise_()
sys.exit(app.exec_())
First I am subclassing QMainWindow. Then QWidget is created. Then assigning it as central via .setCentralWidget(). Next QVBoxLayout() is created and set to mainWidget.
Now creating DockA and DockB. Dialog shows up. But docks are not movable.
What I am doing wrong here?
From the official documentation:
QDockWidget provides the concept of dock widgets, also know as tool palettes or
utilitywindows. Dock windows are secondary windows placed in the
"dock widget area" around the central widget in a QMainWindow
Please refer to the detailed description here and an example here. It is in C++ but you can get an idea of how to do things.
You should use the addDockWidget method of QMainWindow to get fully functional movable dock windows.
Example:
from PySide import QtCore, QtGui
app = QtGui.QApplication([])
window = QtGui.QMainWindow()
window.setCentralWidget(QtGui.QLabel('MainWidget'))
window.addDockWidget(QtCore.Qt.LeftDockWidgetArea, QtGui.QDockWidget('DockA'), QtCore.Qt.Vertical)
window.addDockWidget(QtCore.Qt.LeftDockWidgetArea, QtGui.QDockWidget('DockB'), QtCore.Qt.Vertical)
window.show()
app.exec_()

Categories