qWidget recommended size

40 views
Skip to first unread message

Aren Voorhees

unread,
Mar 27, 2017, 1:51:26 PM3/27/17
to Python Programming for Autodesk Maya
Hey all, I've been trying to figure this out for a bit without luck - I'm trying to find a way to create a recommended or default size for a Pyside widget (windows, buttons, etc...).   I don't want to use something like .setFixedSize() because I still want things to change if the user resizes the window. In all my research I always end up being directed toward .sizeHint(), but I'm having a hard time figuring out how to use it.  In my (incorrect) approach, I'd want to be able to do something like:

self.button.sizeHint(75,30)

But clearly it doesn't work like that.  The documentation says "This property holds the recommended size for the widget.", but I don't get where you input the values to recommend the size?  Does anyone have an example that might help me figure out how to properly set something like this up?

Thanks so much!

Justin Israel

unread,
Mar 27, 2017, 2:32:08 PM3/27/17
to Python Programming for Autodesk Maya


On Tue, Mar 28, 2017, 6:51 AM Aren Voorhees <are...@gmail.com> wrote:
Hey all, I've been trying to figure this out for a bit without luck - I'm trying to find a way to create a recommended or default size for a Pyside widget (windows, buttons, etc...).   I don't want to use something like .setFixedSize() because I still want things to change if the user resizes the window. In all my research I always end up being directed toward .sizeHint(), but I'm having a hard time figuring out how to use it.  In my (incorrect) approach, I'd want to be able to do something like:

self.button.sizeHint(75,30)

sizeHint() is meant to be a computed value. The method is expected to be called at any time to ask what the size hint should be, given the current conditions, such as whether the widget is currently in a layout or has a parent. 

You can also see in the docs that sizeHint() is a virtual method, which implies that it should be reimplemented in order to specify custom logic. You can either implement the method yourself in a subclass, or with this being Python you could dynamically replace the method one-off on any existing instance:

def sizeHint():
    return QSize(75, 30)

self.button.sizeHint = sizeHint

Or

self.button.sizeHint = lambda: QSize(75, 30)


But clearly it doesn't work like that.  The documentation says "This property holds the recommended size for the widget.", but I don't get where you input the values to recommend the size?  Does anyone have an example that might help me figure out how to properly set something like this up?

Thanks so much!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/eb58086b-58fc-4bb3-b228-5644826f1026%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Mar 27, 2017, 4:17:02 PM3/27/17
to python_in...@googlegroups.com

I’ve never had to use sizeHint.

What about using a layout?

import sys
from Qt import QtWidgets
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
long_button = QtWidgets.QPushButton("Looong")
short_button = QtWidgets.QPushButton("Shrt")
layout = QtWidgets.QHBoxLayout(window)
layout.addWidget(long_button, 4)  # Size ratio of 4/1
layout.addWidget(short_button, 1)  # Size ratio of 1/1, long_button takes majority control
window.resize(400, 50)
window.show()

On 27 March 2017 at 19:31, Justin Israel <justin...@gmail.com> wrote:


On Tue, Mar 28, 2017, 6:51 AM Aren Voorhees <are...@gmail.com> wrote:
Hey all, I've been trying to figure this out for a bit without luck - I'm trying to find a way to create a recommended or default size for a Pyside widget (windows, buttons, etc...).   I don't want to use something like .setFixedSize() because I still want things to change if the user resizes the window. In all my research I always end up being directed toward .sizeHint(), but I'm having a hard time figuring out how to use it.  In my (incorrect) approach, I'd want to be able to do something like:

self.button.sizeHint(75,30)

sizeHint() is meant to be a computed value. The method is expected to be called at any time to ask what the size hint should be, given the current conditions, such as whether the widget is currently in a layout or has a parent. 

You can also see in the docs that sizeHint() is a virtual method, which implies that it should be reimplemented in order to specify custom logic. You can either implement the method yourself in a subclass, or with this being Python you could dynamically replace the method one-off on any existing instance:

def sizeHint():
    return QSize(75, 30)

self.button.sizeHint = sizeHint

Or

self.button.sizeHint = lambda: QSize(75, 30)

But clearly it doesn't work like that.  The documentation says "This property holds the recommended size for the widget.", but I don't get where you input the values to recommend the size?  Does anyone have an example that might help me figure out how to properly set something like this up?

Thanks so much!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0uDhWJ1bpFXHq_es6ATJe6WBX_FYQ2u%2BC3_FtZs1zXCQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Mar 27, 2017, 4:53:58 PM3/27/17
to python_in...@googlegroups.com
On Tue, Mar 28, 2017 at 9:17 AM Marcus Ottosson <konstr...@gmail.com> wrote:

I’ve never had to use sizeHint.


sizeHint() is related to layouts, when you have more than one widget with "preferred" size and it needs to figure out what that preferred size is:

"The default policy is Preferred/Preferred, which means that the widget can be freely resized, but prefers to be the size sizeHint() returns."

Implementing the sizeHint() is useful in other situations as well. One is when it is the widget being set into a QDockWidget, where the dock takes the size hint into consideration.

"A QDockWidget acts as a wrapper for its child widget, set with setWidget(). Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title."
 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBE3gFP%2BcJQ_NzWWrD5e0VH6SRYg83g%3Dbwn9gbrLdVEEg%40mail.gmail.com.

Marcus Ottosson

unread,
Mar 27, 2017, 5:49:34 PM3/27/17
to python_in...@googlegroups.com
I understand what it's meant for, I just wanted to add that I've never had to use it, and I've laid out quite a few UIs of various configurations. Just one more data point to consider.​ Layouts and stretch go a long way.

Aren Voorhees

unread,
Mar 28, 2017, 10:50:04 AM3/28/17
to Python Programming for Autodesk Maya
Thanks to both of you - with a bit of messing around, I was able to get both methods working.  

I had always used .addWidget to just add something to the window - good to realize there are arguments to help specify how the add works.

Thanks again.  
Reply all
Reply to author
Forward
0 new messages