Warm tip: This article is reproduced from serverfault.com, please click

PySide

发布于 2020-12-06 12:56:40

I'm learning Python and have been trying to solve an issue on my first widget but I'm struggling.

I made a QGridLayout with 3 rows of 8 square QToolButton. When I scale my main window up or down I want my grid to use the maximum width possible but I also want the buttons to stay square whatever size and ratio my main window has.

So I guess I don't want to use a Fixed sizePolicy on my buttons as I want them to grow or shrink with the window. But I need Them to have a base square size and their ratio to stay the same all the time.

So I was also wondering if I should apply those "keep ratio" infos on the buttons or if it would not be better to apply it directly to the grid (I would say second option is better as I just want the buttons to "fill" the grid cells. And my second interrogation is whether I can achieve that with some sizePolicy only or if I need to add some resizeEvent.

class ColorPalette(QtWidgets.QWidget): 
   def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        
        grid_layout = QtWidgets.QGridLayout()

        self.setLayout(grid_layout)
        grid_layout.setSpacing(0)


        for x in range(3):
                for y in range(8):

                        sizepolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

                        button = QtWidgets.QToolButton()
                        button.setSizePolicy(sizepolicy)

                        grid_layout.addWidget(button, x, y)



class MainWindow(QtWidgets.QWidget): 
   def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        self.layout = QtWidgets.QGridLayout(self)


        self.colorpalette = ColorPalette()
        self.layout.addWidget(self.colorpalette, 0, 0)



c = MainWindow()
c.show()

In this piece of code I was trying to layer the widgets like this "MainWindow that can be resized in any way we want" -> "ColorPalette Grid that will follow the MainWindow resize but with some keeping ratio constraints" -> "Buttons that fill the cells"

But I need some help on setting the ColorPalette Grid size constraints.

Thanks for your help!

Questioner
Rainbowrama
Viewed
1
Rainbowrama 2020-12-07 05:26:22

Kind of found a solution with the resize event..

class ColorPalette(QtWidgets.QWidget): 
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

    def resizeEvent(self, event):

        # Define Number of Rows
        r = 3
        # Define Number of Columns
        c = 8
        # Define Button Minimum Size
        s = 10

        self.setMinimumSize(c*2*s,r*2*s)
        new_size = QtCore.QSize(c*s, r*s)
        new_size.scale(event.size(), QtCore.Qt.KeepAspectRatio)
        self.resize(new_size)
        grid = QtWidgets.QGridLayout()
        self.setLayout(grid)
        grid.setSpacing(0)


        for x in range(r):
                for y in range(c):

                        button = QtWidgets.QToolButton()

                        policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
                        button.setSizePolicy(policy)
                        button.setMinimumSize(s, s)

                        grid.addWidget(button, x, y)



class MainWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
       QtWidgets.QWidget.__init__(self, parent)

       layout = QtWidgets.QHBoxLayout()
       self.custom_widget = ColorPalette()
       layout.addWidget(self.custom_widget)
       self.setLayout(layout)




c = MainWidget()
c.show()