Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Widgets with SizePolicy there is no effects
QtWS25 Last Chance

Widgets with SizePolicy there is no effects

Scheduled Pinned Locked Moved Unsolved General and Desktop
pyqt5qsizepolicyqwidget
5 Posts 4 Posters 1.7k Views
  • 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 Offline
    P Offline
    Pythonic person
    wrote on 25 Jan 2022, 14:50 last edited by
    #1

    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?

    J 1 Reply Last reply 25 Jan 2022, 14:53
    0
    • P Pythonic person
      25 Jan 2022, 14:50

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Jan 2022, 14:53 last edited by
      #2

      @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 25 Jan 2022, 15:05
      2
      • J jsulm
        25 Jan 2022, 14:53

        @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.

        P Offline
        P Offline
        Pythonic person
        wrote on 25 Jan 2022, 15:05 last edited by
        #3

        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?

        C 1 Reply Last reply 25 Jan 2022, 15:19
        0
        • P Pythonic person
          25 Jan 2022, 15:05

          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?

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 25 Jan 2022, 15:19 last edited by
          #4

          @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 26 Jan 2022, 04:02 last edited by
            #5

            @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
            3

            1/5

            25 Jan 2022, 14:50

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved