Creating a transparent overlay with qt
Asked Answered
B

2

12

I've been learning python recently and now I wanted to (try to) create my first real application, a subtitle player for Linux. So far I've been using the Greenfish subtitle player, which is aimed at Windows users and not properly working in Linux.

I wanted to create the application in qt, since I discovered that transparent windows are not possible in tkinter, but if anybody knows a better framework please suggest!

Now before starting I've been researching the web for several hours to discover how to get my application to show over a full screened flash video and it seems like this is not possible. However the aforementioned GF subtitle player manages to do so in Windows, but not in Linux(maybe it's also because it's running through wine).

So my question is it possible to create a transparent application that remains over a fullscreened flash video and if so, could you point me in the right direction?

Thanks in advance.

edit: here some example code I've been trying. The window produced by this piece of code does not stay above a fullscreened video

import sys
from PyQt4 import QtGui, QtCore

class mymainwindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)

app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
mywindow.show()
Bran answered 20/9, 2014 at 14:57 Comment(4)
There should be no problem in getting a transparent window as a partial overlay that's on top of the screen stack. All you need to do in Qt is to set a transparent window background color (through a stylesheet, for example), and raise your window so that it's on top of everything else. I don't know where you looked, but QWidget documentation has everything you need. In fact, a subtitle "player" would be around a 100 lines in Qt :)Eritrea
Well so far I tried: QtCore.Qt.WindowStaysOnTopHint which is setting the window above all other windows, however it disappears behind a fullscreened video. Do I have to put it on top after the video is set to fullscreen mode?Bran
As the previous commenter suggested, this should be a fairly trivial thing to accomplish. Please post a MCV example that demonstrates the problems you are having rather than forcing people to guess what is wrong with your code.Kainite
sorry, I don't have yet started working on code, I was trying this piece of code I edited into my initial post.Bran
K
8

The example code below will create a centred, frameless window that should stay on top of all other windows on Linux (you can click on the window to close it).

import sys
from PyQt4 import QtGui, QtCore

class mymainwindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowFlags(
            QtCore.Qt.WindowStaysOnTopHint |
            QtCore.Qt.FramelessWindowHint |
            QtCore.Qt.X11BypassWindowManagerHint
            )
        self.setGeometry(QtGui.QStyle.alignedRect(
            QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
            QtCore.QSize(220, 32),
            QtGui.qApp.desktop().availableGeometry()))

    def mousePressEvent(self, event):
        QtGui.qApp.quit()

app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
Kainite answered 21/9, 2014 at 0:32 Comment(3)
this work only if the game is windowed, not in fullscreen mode.Culch
@Culch did you find any solutions to create an overlay for fullscreen applications?Probst
you must use some external overlay library, based on OpenGL or directx.directdrawoverlaylib.codeplex.comCulch
H
16

Update for PyQt5 pip install PyQt5

import sys

from PyQt5 import QtGui, QtCore, uic
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowFlags(
            QtCore.Qt.WindowStaysOnTopHint |
            QtCore.Qt.FramelessWindowHint |
            QtCore.Qt.X11BypassWindowManagerHint
        )
        self.setGeometry(
            QtWidgets.QStyle.alignedRect(
                QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
                QtCore.QSize(220, 32),
                QtWidgets.qApp.desktop().availableGeometry()
        ))

    def mousePressEvent(self, event):
        QtWidgets.qApp.quit()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywindow = MainWindow()
    mywindow.show()
    app.exec_()
Hathaway answered 1/2, 2020 at 18:13 Comment(0)
K
8

The example code below will create a centred, frameless window that should stay on top of all other windows on Linux (you can click on the window to close it).

import sys
from PyQt4 import QtGui, QtCore

class mymainwindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowFlags(
            QtCore.Qt.WindowStaysOnTopHint |
            QtCore.Qt.FramelessWindowHint |
            QtCore.Qt.X11BypassWindowManagerHint
            )
        self.setGeometry(QtGui.QStyle.alignedRect(
            QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
            QtCore.QSize(220, 32),
            QtGui.qApp.desktop().availableGeometry()))

    def mousePressEvent(self, event):
        QtGui.qApp.quit()

app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
Kainite answered 21/9, 2014 at 0:32 Comment(3)
this work only if the game is windowed, not in fullscreen mode.Culch
@Culch did you find any solutions to create an overlay for fullscreen applications?Probst
you must use some external overlay library, based on OpenGL or directx.directdrawoverlaylib.codeplex.comCulch

© 2022 - 2024 — McMap. All rights reserved.