PyQt5 - FramelessWindowHint& WindowStaysOnTopHint不起作用

时间:2018-05-26 22:46:16

标签: python python-3.x pyqt pyqt4 pyqt5

我希望我的PyQt5程序无框架并始终位于屏幕顶部。当我使用WindowStayOnTopHint时,它工作正常,但是当我将它与FramelessWindowHint一起使用时,它变为无框架,但不会停留在屏幕顶部。在做了一些研究之后,我找到了this,并且说它试图使用setMask,但我无法让它工作。

这是我的代码:

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QLabel


class Invisible(QLabel):
 def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.__press_pos = None
    self.initUI()

 def initUI(self):
    self.setWindowFlags(Qt.WindowStaysOnTopHint)
    self.setMask() #This is where I use the setMask function, but it raises an error

    self.setAttribute(Qt.WA_TranslucentBackground)
    self.setText("Drag me...")
    self.setFont(QFont("Times", 50, QFont.Bold))
    self.adjustSize()  
    self.move(QApplication.instance().desktop().screen().rect().center()
              - self.rect().center())

 def mousePressEvent(self, event):
    if event.button() == Qt.LeftButton:
        self.__press_pos = event.pos()  

 def mouseReleaseEvent(self, event):
    if event.button() == Qt.LeftButton:
        self.__press_pos = None

 def mouseMoveEvent(self, event):
    if self.__press_pos:  
        self.move(self.pos() + (event.pos() - self.__press_pos))


 def main():
    app = QApplication(sys.argv)
    w = Invisible()
    w.show()
    return app.exec_()


if __name__ == '__main__':
   sys.exit(main())

此代码给出了错误:

TypeError: arguments did not match any overloaded call:
  setMask(self, QBitmap): not enough arguments
  setMask(self, QRegion): not enough arguments

它要求更多的论点,但当我给它更多的论据时,它说有太多的论点。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您的问题是您必须使用运算符|

激活这两个属性
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)

完整代码:

import sys

from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QLabel, QStyle


class Invisible(QLabel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__press_pos = QPoint()
        self.initUI()

    def initUI(self):
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setText("Drag me...")
        self.setFont(QFont("Times", 50, QFont.Bold))
        self.adjustSize()
        self.setGeometry(
            QStyle.alignedRect(
                Qt.LeftToRight,
                Qt.AlignCenter,
                self.size(),
                QApplication.instance().desktop().availableGeometry()
                )
            )

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.__press_pos = event.pos()  

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.__press_pos = QPoint()

    def mouseMoveEvent(self, event):
        if not self.__press_pos.isNull():  
            self.move(self.pos() + (event.pos() - self.__press_pos))

def main():
    app = QApplication(sys.argv)
    w = Invisible()
    w.show()
    return app.exec_()


if __name__ == '__main__':
   sys.exit(main())

enter image description here

setMask()用于为窗口小部件提供不同的边框,例如example我获得了以下内容:

enter image description here