Issue
trying to get a custom widget to expand to parent and it's only doing what I want when my widgets are inheriting from QLabel instead of QWidget. I have tried setting QSizePolicy but it still doesn't seem to be working
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from mywidgets import MenuLabel, MenuWidget, MainWidget
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("InstaPlus")
outerlayout = QHBoxLayout()
outerlayout.setContentsMargins(0, 0, 0, 0)
outerlayout.setSpacing(0)
menulayout = QVBoxLayout()
menulayout.setContentsMargins(0, 0, 0, 0)
menulayout.setSpacing(0)
menuwidget = MenuWidget()
mainwidget = MainWidget()
menulayout.addWidget(MenuLabel("Menu Item 1"))
menulayout.addWidget(MenuLabel("Menu Item 2"))
menulayout.addWidget(MenuLabel("Menu Item 3"))
menulayout.addWidget(MenuLabel("Menu Item 4"))
menulayout.addWidget(MenuLabel("Menu Item 5"))
menulayout.addStretch()
menuwidget.setLayout(menulayout)
outerlayout.addWidget(menuwidget)
outerlayout.addWidget(mainwidget)
self.setLayout(outerlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = Window()
main.resize(800, 500)
main.show()
sys.exit(app.exec_())
Here is my mywidgets code
from PyQt5.QtWidgets import QLabel, QWidget, QSizePolicy
class MenuLabel(QLabel):
def __init__(self, title):
super().__init__()
self.title = title
self.setText(self.title)
self.setStyleSheet(open("styles.css").read())
class MenuWidget(QWidget):
def __init__(self):
super().__init__()
self.setAccessibleName("menu")
self.setStyleSheet(open("styles.css").read())
self.setFixedWidth(200)
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.setAccessibleName("main")
self.setStyleSheet(open("styles.css").read())
Here is the layout when inheriting from QWidget
and this is it when my widgets are inheriting from QLabel
How can I get my widgets to inherit from Qwidget and and have them resize like a QLabel?
Here is the styles file
MenuLabel{
background-color: #484848;
color: #dadada;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
padding-right: 100px;
font-size: 10pt;
font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Geneva', 'Verdana', sans-serif
}
MenuLabel:hover{
background-color: rgba(8, 3, 9, 0.65);
}
[accessibleName="menu"]{
background-color: #484848;
}
[accessibleName="main"]{
background-color: #2e2e2e;
}
Solution
The size of your MenuWidget
class is exactly the same whenever you subclass from QWidget or QLabel.
The problem is that when subclassing from QWidget it's not enough to set the stylesheet. As the official stylesheet reference documentation explains:
If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget.
class MenuWidget(QWidget):
# ...
def paintEvent(self, event):
opt = QtWidgets.QStyleOption()
opt.initFrom(self)
qp = QtGui.QPainter(self)
self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, qp, self)
If you're using accessibleName()
just as a stylesheet selector, do consider that it's not a good choice, as the accessible name can (should) be localized and is not the right method to identify a class in stylesheets. Use the objectName()
instead, and the #objectName {}
selector instead. See the selector types chapter in the stylesheet syntax documentation.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.