3

I am using PyQt5 and I am trying to prevent push button from resizing automatically. So I used this code to achieve this. My goal was if I create a button with fixed size, it won't resize on its own. So I wrote the following code:

rect = QtCore.QRect()
rect.setSize(QtCore.QSize(5, 80))
button.setGeometry(rect)

But it doesn't work. The same resize issue is still there. What is going wrong there?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Man_From_India
  • 207
  • 1
  • 4
  • 10
  • It has got its own BoxLayout defaulting to expand. Add vertical or horizontal [Spacer](https://doc.qt.io/qt-5/qspaceritem.html) with policy Expanding in the same BoxLayout and that spacer will take all the space widget has got, leaving your button with defined size. – ipaleka Jul 10 '19 at 17:01
  • @ipaleka I've checked the official documentation, but it doesn't mention it. Where from will I get the complete info? And how did you come to know? I'm asking because I am very new and want to learn. Please help. – Man_From_India Jul 10 '19 at 18:03
  • 1
    Okay you had to run your code inside of something in order to have an issue with resizing the button but what you have provided cannot be ran nor does it even have a button in it. We should be able to simply copy/paste the code you submitted and reproduce your issue. Of course I think the link to the minimal reproducible example explains all that did you even read that? – Dennis Jensen Jul 10 '19 at 18:13
  • @DennisJensen well I have no idea what you are pointing at? Yes I will provide you the full code ai wrote, of course very small, because it is for learning and demo purpose. But I can't make out the reason of the issue and I really didn't get where from to extract the info the first commenter derived. I will paste better code, please give me some time. – Man_From_India Jul 11 '19 at 00:54

1 Answers1

2

If you want a widget to maintain its size then you must use setFixedSize()

button.setFixedSize(QtCore.QSize(5, 80))

If your button is in a layout it will use the sizePolicy to determine the size behavior, in the case of the button it is:

Therefore, the button is generally resized horizontally, and not vertically.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you. It makes sense. On top of that you have included in your answer the exact location of the official documentation where the particular topics are discussed. Thank you so much. It helps a lot to beginners. – Man_From_India Jul 11 '19 at 05:09
  • What if it is not in a layout? – Man_From_India Jul 12 '19 at 03:35
  • @Man_From_India Then the size of the button should not change, and if a size is not set the sizeHint() is used, if a size is set this will take into account the sizeHint() and minimumSizeHint() – eyllanesc Jul 12 '19 at 03:40
  • but as per the documentation on Qt it says: (Size Policy) If there is a QLayout that manages this widget's children, the size policy specified by that layout is used. If there is no such QLayout, the result of this function is used. doc.qt.io/qt-5/… – Man_From_India Jul 12 '19 at 05:16
  • @Man_From_India I think you are not understanding the docs, I recommend you experiment and then you can better understand the docs. In the beginning it can be confusing (at the beginning I also had that problem) but as long as you implement tests everything will be clarified. – eyllanesc Jul 12 '19 at 05:19
  • @Man_From_India On the other hand if you have any doubt always creates a MWE (minimal workable example) to analyze it better because many times we use technical words incorrectly, many times it has happened to me that the OP uses a certain phrase that in general has many meanings but we are governed by the Qt docs have a more precise meaning. – eyllanesc Jul 12 '19 at 05:22
  • nods but it is confusing. For example if we create a layout like layout = Qlayout () and then layout.addwidget(button). This is what the doc says I think when it says Qlayout manages the widget's children. – Man_From_India Jul 12 '19 at 05:32
  • @Man_From_India an MWE is: `w = QWidget()` `lay = QVBoxLayout(w)` `button = QPushButton()` `lay.addWidget(button)`, the layout was set to w so the parent button is added to the layout of the button is w so the geometry of the button will be handled by the layout, if instead you create another button `btn2 = QPushButton(w)` even btn2 is a child of w but its geometry will not be handled by lay. – eyllanesc Jul 12 '19 at 05:38
  • @Man_From_India If you have another doubt I recommend you create a question providing an MWE or MRE if necessary, these comments have already been extended too, goodbye. – eyllanesc Jul 12 '19 at 05:39
  • in first case lay is set to w. So lay is the child of w. And button is the child of lay? And the geometry of the button is to be handled by the layout. I got this. I think this is what u meant, right? – Man_From_India Jul 12 '19 at 05:47
  • @Man_From_India No, it is not a child of the layout, it is the child of the widget where the layout was established, that is, it is the child of w. – eyllanesc Jul 12 '19 at 05:49
  • sorry problem is I am barred from asking any more questions. – Man_From_India Jul 12 '19 at 05:50
  • @Man_From_India Basic widgets rule: 1) A widget is only part of a window if it is a child of the window or child of a widget that is part of the window. 2) Layouts are not widgets or visible elements, they only handle their geometry. In conclusion any widget added to the layout is the child of the widget where the layout was established. – eyllanesc Jul 12 '19 at 05:52
  • @Man_From_India Well, then earn more reputation so you have more privileges. I will not answer anything else, goodbye. – eyllanesc Jul 12 '19 at 05:53