3

I'm working on a custom ui in Maya 2018 which uses PyQt5 (via PySide2 - there are some minimal differences, but it's essentially PyQt5).

I've got a QHBoxLayout with two widgets in it and their sizing policies are both set to 'expanding'. I set the minimum width of one of them to be about twice as wide as the other.

Whenever I expand the window, the smaller widget expands until it's the same size as the larger one (which doesn't change size at all)... and then they both continue to expand at the same rate - both taking up half of the window.

I would prefer they maintain their sizes relative to each other throughout the whole expansion.

Is there a way to do this that I'm just completely overlooking?

Here's some simplified code I whipped up to reproduce the issue:

import PySide2.QtWidgets as QtWidgets

class TestDialog(QtWidgets.QDialog):
    def __init__(self, *args, **kwargs):
        QtWidgets.QDialog.__init__(self, *args, **kwargs)

        main_layout = QtWidgets.QHBoxLayout()
        self.setLayout(main_layout)
        main_layout.setContentsMargins(5, 5, 5, 5)
        main_layout.setSpacing(5)

        w1 = QtWidgets.QPushButton('small')
        w2 = QtWidgets.QPushButton('large')
        w1.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        w2.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        w2.setMinimumWidth(250)

        main_layout.addWidget(w1)
        main_layout.addWidget(w2)

        self.setMinimumWidth(450)
        self.setMinimumHeight(100)
        self.resize(450, 100)


test = TestDialog()
test.show()
silent_sight
  • 492
  • 1
  • 8
  • 16
  • 1
    could you post your codes? – Sachith Muhandiram Nov 02 '19 at 23:34
  • 2
    Or you use PyQt5 or you use PySide2 since they are different packages. On the other hand it provides a [mre] – eyllanesc Nov 02 '19 at 23:54
  • Indicate in the edition the question that PySide2 should use since Maya uses it natively, PyQt5 and PySide2 are different since the last one currently has a lot of bugs that could influence a possible solution. – eyllanesc Nov 03 '19 at 01:13
  • Do you want the ratio between the widths of the widgets to be 1 to 2? – eyllanesc Nov 03 '19 at 02:11
  • It's not necessarily 1 to 2, just somewhere around there (I'm currently experimenting with what the best size difference is going to be)... Do I need to figure out the exact mathematical ratio? – silent_sight Nov 03 '19 at 02:21
  • @silent_sight mmm, I don't understand. What exactly do you want? Did my answer work for you ?, please use `@username` – eyllanesc Nov 03 '19 at 03:01
  • After playing around with setting the stretch, I found values of 4 and 5 worked out for me, and setting the minimum width would make the stretching still act funky. To clarify my question on the comment, I was asking if I had to calculate the width of the other widget, then determine the actual ratio between the two... I don't think 4 and 5 stretch values made the larger widget have a minimum of exactly 250 pixels, but it's not crucial it does... – silent_sight Nov 05 '19 at 19:09

1 Answers1

6

If you want the relationship between the widths to be maintained then you must set the stretch by adding the widgets:

from PySide2 import QtWidgets


class TestDialog(QtWidgets.QDialog):
    def __init__(self, *args, **kwargs):
        super(TestDialog, self).__init__(*args, **kwargs)

        main_layout = QtWidgets.QHBoxLayout(self)

        main_layout.setContentsMargins(5, 5, 5, 5)
        main_layout.setSpacing(5)

        w1 = QtWidgets.QPushButton("small")
        w2 = QtWidgets.QPushButton("large")
        w1.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
        )
        w2.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
        )

        main_layout.addWidget(w1, stretch=1)
        main_layout.addWidget(w2, stretch=2)

        self.setMinimumSize(450, 100)
        self.resize(450, 100)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    test = TestDialog()
    test.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Thanks! Setting the stretch works great - I just had to play with those values to get it to have a ratio I liked... I also found that if you're using this, you should not set a minimum width and just let the stretch take care of it. – silent_sight Nov 05 '19 at 19:11