簡體   English   中英

pyqt5 無法將邊框應用於無框窗口

[英]pyqt5 can't apply border to framelesswindow

我通過 pyqt5 中的 setStyleSheet 函數應用了邊框寬度和邊框顏色。 但是當我在無框 window 中使用它時,沒有應用邊框。 我的代碼有什么問題嗎? 我需要幫助。 我的代碼來了。

import sys
from PyQt5.QtWidgets import QWidget,QApplication
from PyQt5.QtCore import Qt,QPoint

class CommonFramelessWidget(QWidget):
    def __init__(self,parent):
        super(CommonFramelessWidget,self).__init__(parent)
        self.setWindowFlags(Qt.Window|Qt.FramelessWindowHint)
        self.setStyleSheet('border:5px  black; background-color:red;')

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()
    def mouseMoveEvent(self,e):
        delta = QPoint (e.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = e.globalPos()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = CommonFramelessWidget(None)
    mw.show()
    sys.exit(app.exec_())

除了激活 Qt::WA_StyledBackground 屬性之外,您還必須設置邊框類型,以便它使用樣式表的信息作為背景樣式:

import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt, QPoint


class CommonFramelessWidget(QWidget):
    def __init__(self, parent=None):
        super(CommonFramelessWidget, self).__init__(parent)
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)
        self.setStyleSheet("border: 5px solid black; background-color:red;")
        self.setAttribute(Qt.WA_StyledBackground)

        self.resize(640, 480)

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()
        super(CommonFramelessWidget, self).mousePressEvent(event)

    def mouseMoveEvent(self, e):
        delta = QPoint(e.globalPos() - self.oldPos)
        self.move(self.pos() + delta)
        self.oldPos = e.globalPos()
        super(CommonFramelessWidget, self).mouseMoveEvent(e)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = CommonFramelessWidget()
    mw.show()
    sys.exit(app.exec_())

試試看:

import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt, QPoint

class CommonFramelessWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.Window| Qt.FramelessWindowHint)
        
#        self.setStyleSheet('border: 5px black; background-color:red;')        
        self.widget = QWidget(self)
        self.widget.setStyleSheet('.QWidget {border: 5px solid rgb(255, 0, 0); background-color: blue;}')
        self.widget.setFixedSize(700, 500)

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()
        
    def mouseMoveEvent(self,e):
        delta = QPoint (e.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = e.globalPos()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = CommonFramelessWidget()
    mw.show()
    sys.exit(app.exec_())

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM