Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved Widgets with SizePolicy there is no effects

    General and Desktop
    pyqt5 qsizepolicy qwidget
    4
    5
    317
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Pythonic person last edited by

      Why the sizePolicy doesn't affect widgets that aren't in layout?

      here is an example:

      from PyQt5 import QtWidgets
      
      app = QtWidgets.QApplication([])
      window = QtWidgets.QWidget()
      window.setGeometry(50, 50, 500, 300)
      
      test_widget = QtWidgets.QWidget(window)
      test_widget.setMinimumSize(100, 100)
      test_widget.setStyleSheet("background-color:red")
      size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
      test_widget.setSizePolicy(size_policy)
      
      
      window.show()
      app.exec()
      

      But that doesn't work, if you changed the main window size the red box still has the same size.

      So how can I make that red box resizeable when the parent (main window) is changing?.
      Can I achieve that without using layouts?

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Pythonic person last edited by

        @Pythonic-person said in Widgets with SizePolicy there is no effects:

        Can I achieve that without using layouts?

        Why don't you want to use layouts?
        If you do not use layouts then you have to size and position your widgets by yourself.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply Reply Quote 2
        • P
          Pythonic person @jsulm last edited by

          Why don't you want to use layouts?

          Actually, Because I added another widget to that layout. And I want to add Canvas to the main widget not in that layout so I can have: Canvas in the back, and other menus in front (Adding canvas to that widget allow canvas taking the whole size of that widget)

          @jsulm said in Widgets with SizePolicy there is no effects:

          If you do not use layouts then you have to size and position your widgets by yourself.

          So the sizePolicy won't work, right? Then how can I achieve that? Do you mean taking the main window size and then change the widget size with the code?

          Christian Ehrlicher 1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion @Pythonic person last edited by

            @Pythonic-person said in Widgets with SizePolicy there is no effects:

            So the sizePolicy won't work, right? Then how can I achieve that?

            By using layouts

            Qt has to stay free or it will die.

            1 Reply Last reply Reply Quote 0
            • C
              ChrisW67 last edited by

              @Pythonic-person said in Widgets with SizePolicy there is no effects:

              Do you mean taking the main window size and then change the widget size with the code?

              You either use one of the provided layout mechanisms, develop you your only layout implementation, or you do all the resizing of contained widgets yourself 1990's style.

              If you wish to do it yourself then you need to override the resizeEvent() of the container widget and resize the contained widgets yourself using whatever logic you see fit.

              1 Reply Last reply Reply Quote 2
              • First post
                Last post