pyqt5 border only apply to parent widget问题

pyqt5 border apply only to parent widget problem

各位。我在 pyqt5 中遇到了奇怪的问题。 我尝试更改边框但失败了,我在应用边框样式时总是很头疼。 我的代码来了。请帮助我

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



class SomeWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setStyleSheet('border:10px solid green')
        pass

class mainWidget(QWidget):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        widget = SomeWidget(self)
        widget.setWindowFlags(Qt.FramelessWindowHint|Qt.Window)
        widget.resize(self.width()//2,self.height()//2)
        
        widget.show()
        # self.setStyleSheet(styles)
        pass

def test1():
    return 0,1

if __name__ == "__main__":

    app = QApplication(sys.argv)
    mw = mainWidget()
    mw.show()
    sys.exit(app.exec_())
    
    pass

这里的问题是 QWidget class 不支持边框 属性(参见 this answer and the Qt stylesheet documentation directly)。

也就是说,将 QWidget 更改为 QFrame 就可以了:

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QFrame


class SomeWidget(QFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setStyleSheet('border:10px solid green')


class mainWidget(QWidget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        widget = SomeWidget(self)
        widget.setWindowFlags(Qt.FramelessWindowHint | Qt.Window)
        widget.resize(self.width() // 2, self.height() // 2)

        widget.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = mainWidget()
    mw.show()

    sys.exit(app.exec_())

试一试:

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QTextEdit
from PyQt5.QtCore import Qt
from PyQt5.Qt import *


class SomeWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
#        self.setStyleSheet('background: #0ff; border: 10px solid green')
        
        self.widget = QWidget(self)
        self.widget.setObjectName('Custom_Widget')
        self.widget.setStyleSheet('''
            QWidget#Custom_Widget {
                background: rgba( 255, 155, 155, 170);
                border-radius: 20px;
                border: 10px solid green;                   
            }
        ''')

        self.v = QVBoxLayout(self)
        self.v.setContentsMargins(0, 0, 0, 0)
        self.v.addWidget(self.widget)


class MainWidget(QWidget):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        widget = SomeWidget(self)
        widget.setWindowFlags(Qt.FramelessWindowHint | Qt.Window)
        widget.setAttribute(Qt.WA_TranslucentBackground)                # +++
        widget.resize(self.width()//2, self.height()//2)
        
        widget.move(100, 100)                                           # +++
        widget.show()
        
        self.setStyleSheet('background:#00f;')


def test1():     # ???
    return 0,1

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