Creating a transparent overlay with qt

2020-02-26 02:19发布

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()

2条回答
你好瞎i
2楼-- · 2020-02-26 02:47

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_()
查看更多
够拽才男人
3楼-- · 2020-02-26 02:54

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_()
查看更多
登录 后发表回答