python - Why does PySide6.QtWidgets.QVBoxLayout not expand despite setting QSizePolicy.Expanding - Stack Overflow

Why does PySide6.QtWidgets.QVBoxLayout not expand despite setting QSizePolicy.Expanding? Additionally,

Why does PySide6.QtWidgets.QVBoxLayout not expand despite setting QSizePolicy.Expanding? Additionally, on the first click, even though the items have been added to the Legend, then items aren't visible until the mouse is clicked again. But when the 2nd click occurs, the 1st box appears but is squished with the 2nd like the image below.

But after subsequent clicks, there is less squish

Why does this occur? I have tried to set the Legend with resize(200,200), but increasing the size doesn't change anything.

import pyqtgraph.opengl as gl
from PySide6 import QtCore, QtWidgets
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtGui import QColor, QPixmap, QPainter
import sys

class CustomGLViewWidget(gl.GLViewWidget):    
    def __init__(self, legend=None, *args, **kwargs):
        super().__init__(*args, **kwargs)   
        self.colours = [(0,1,1,1), (1,1,1,1), (1,0,0,1), (1,0,1,1), (1,1,0,1)]
        self.idx=0
        self.legend = legend

    def mousePressEvent(self, ev):
        print(ev.buttons().value)
        if ev.buttons().value <= 2:
            self.legend.add_legend_item(f"{self.idx}00", self.colours[self.idx])
            self.idx+=1
        return super().mousePressEvent(ev)

class ColorSquare(QtWidgets.QLabel):
    def __init__(self, color, size=50):
        super().__init__()
        self.color = color
        self.color_width = size
        self.color_height = size/2
        self.setFixedSize(self.color_width, self.color_height)
        self.update_color()

    def update_color(self):
        pixmap = QPixmap(self.color_width, self.color_height)
        pixmap.fill(QtCore.Qt.transparent)
        painter = QPainter(pixmap)        
        painter.setBrush(QColor.fromRgbF(*self.color))
        painter.drawRect(0, 0, self.color_width, self.color_height)
        painter.end()
        self.setPixmap(pixmap)
        self.setStyleSheet("border: 2px solid black;")
        
class Legend(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setStyleSheet("background: transparent; color: white;")
        self.layout = QtWidgets.QVBoxLayout(self)
        self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.resize(200,200)
        
    def add_legend_item(self, legend_name, colour):
        hbox_layout = QtWidgets.QHBoxLayout()

        hbox_layout.setAlignment(QtCore.Qt.AlignLeft)
        hbox_layout.setContentsMargins(0, 0, 0, 0)
        hbox_layout.setSpacing(5)

        square_colour = ColorSquare(colour)
        hbox_layout.addWidget(square_colour)

        square_label = QtWidgets.QLabel(legend_name)
        square_label.setStyleSheet("border: 1px solid white; padding: 3px; background-color: white; color: black")
        square_label.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        
        hbox_layout.addWidget(square_label)
        inner_widget = QtWidgets.QWidget()
        inner_widget.setLayout(hbox_layout)
        self.layout.addWidget(inner_widget)
        self.adjustSize()
        self.update()

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)   
        self.setGeometry(0, 0, 600, 600)
        self.widget_central = QtWidgets.QWidget()
        self.setCentralWidget(self.widget_central)
        self.widget_layout_v_main = QtWidgets.QVBoxLayout(self.widget_central)
        legend=Legend()
        self.widget_gl = CustomGLViewWidget(legend)
        legend.setParent(self.widget_gl)
        self.widget_layout_v_main.addWidget(self.widget_gl)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec())

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744580861a4582027.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信