Creating a transparent overlay with qt

穿精又带淫゛_ 提交于 2020-02-12 16:44:39

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/25950049/creating-a-transparent-overlay-with-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!