简体   繁体   English

使用 qt 创建透明叠加层

[英]Creating a transparent overlay with qt

I've been learning python recently and now I wanted to (try to) create my first real application, a subtitle player for Linux.我最近一直在学习 python,现在我想(尝试)创建我的第一个真正的应用程序,一个适用于 Linux 的字幕播放器。 So far I've been using the Greenfish subtitle player, which is aimed at Windows users and not properly working in Linux.到目前为止,我一直在使用 Greenfish 字幕播放器,它是针对 Windows 用户的,在 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!我想在 qt 中创建应用程序,因为我发现在 tkinter 中无法使用透明窗口,但是如果有人知道更好的框架,请提出建议!

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.现在在开始之前,我已经研究了几个小时的网络,以发现如何让我的应用程序显示在全屏 Flash 视频上,但似乎这是不可能的。 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).然而,前面提到的 GF 字幕播放器在 Windows 中设法这样做,但在 Linux 中却没有(也许也是因为它通过 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?所以我的问题是有没有可能创建一个透明的应用程序,保留在全屏 Flash 视频上,如果是这样,你能指出我正确的方向吗?

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

Update for PyQt5 pip install PyQt5更新 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_()

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).下面的示例代码将创建一个居中的无框窗口,该窗口应位于 Linux 上所有其他窗口的顶部(您可以单击该窗口将其关闭)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM