pyqt5 border apply only to parent widget problem

1k Views Asked by At

Everyone. I have strange issue in pyqt5. I tried to change border but failed, I have always headache when applying border style. Here comes my code. Pls help me

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
2

There are 2 best solutions below

0
On BEST ANSWER

The issue here is that the QWidget class does not support the border property (see this answer and the Qt stylesheet documentation directly).

That being said, changing the QWidget to a QFrame does the trick:

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

Try it:

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

enter image description here