K.Mulier
K.Mulier

Reputation: 9640

Scaling QWidgets in PyQt

I have built an application in Python, using the PyQt4 library for my GUI. My GUI plots a graph and some buttons above it. The graph lives in frame_B, the buttons (and the led) above the graph live in frame_A:

enter image description here

I create frame_A (and also frame_B) in my MainWindow class:

class CustomMainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(CustomMainWindow, self).__init__()
        # Do some initialization stuff..
    ''''''

    def initLayout(self):
        # Create frame_A and its corresponding layout
        frame_A = QtGui.QFrame(self)
        setCustomSize(frame_A, 380,800)
        self.layout_A = QtGui.QGridLayout()
        frame_A.setLayout(self.layout_A)

        # Create frame_B, and so forth..

    ''''''

    def setButtons(self):
        # Create btn01..
        # Create btn02..
        # Create btn03..
        # Create btn04..
        # Create btn05..
        # Create led01..
        # Add all buttons to the layout of frame_A:
        self.layout_A.addWidget(self.btn01, *(0,0))
        self.layout_A.addWidget(self.btn02, *(0,1))
        self.layout_A.addWidget(self.btn03, *(0,2))
        self.layout_A.addWidget(self.btn04, *(0,3))
        self.layout_A.addWidget(self.btn05, *(0,4))
        self.layout_A.addWidget(self.led01, *(0,5))

    ''''''

''''''

The result is very satisfying, as you can see in the figure above. When I enlarge my window (by a simple click and drag with the mouse), frame_A and frame_B also enlarge. That is perfectly fine. I wouldn't want it any other way.

But it bothers me that the buttons in frame_A get evenly distributed to fill all the new space. I would prefer a behaviour like this: enter image description here

Anyone has a clever idea to accomplish just that?

Upvotes: 1

Views: 1306

Answers (2)

ngulam
ngulam

Reputation: 1075

add self.layout_A.addStretch() after last button

Upvotes: 2

tobilocker
tobilocker

Reputation: 981

If you only want to add Widgets horizontally i would suggest using QHBoxLayout instead of QGridLayout. With that you could use layout_A.setAlignment(QtCore.Qt.AlignLeft). Not sure if that also works for a Gridlayout.

Upvotes: 1

Related Questions