PDA

View Full Version : Small QPushButton possible? (less than 75 units)



masterlaws
3rd December 2009, 18:30
I am having a very difficult time trying to create a small qpushbutton. This is a simple issue that possibly ties into other issues I am encountering. I figured I would start with this simple problem and go from there.

So, how do you make a small QPushButton in designer without explicitly setting it's maximumWidth? I want the layout (which the button is a member of) to shrink the button to a size that fits the button's text. The text might be only one or two characters long, so this is a SMALL button. I am only concerned with horizontal width (as opposed to vertical)

Here is a simple setup in designer

I have a qwidget as the main window.
I have a horizontal layout set for the window
I have qpushbutton in the layout (with button text 'X')
I might optionally have a horizontal spacer (if it helps with the problem)

For some reason, buttons do not get any smaller than 75 units (in width) unless explicitly told to do so (i.e through a geometry call or maximumWidth call). Does anyone know where this 75 unit rule is being made and how I might be able to get around it?

You can see this issue very clearly in designer by placing a button in a window, and give it a simple text name like 'X', and then tell it to Adjust Size (ctrl+j). The button will never shrink down to its contents. It always stops at 75 pixels. I don't believe that this is a designer issue. I have tried writing the gui code from scratch and have the same problems.

Can anyone enlighten me as to what is going on?

Thanks :)

wysota
4th December 2009, 00:28
Use QToolButton instead of QPushButton. Push buttons are meant to be wide.

masterlaws
4th December 2009, 19:37
Well that sure is an easier route! Thanks!
It doesn't quite explain to me the technical limitations of the qpushbutton, but I am comfortable using another widget instead. I had not used QToolButton before. Is there any reason not to use this button for most of my QPushButton needs? What's the drawback? They both inherit from QAbstractButton, so they must be quite similar.

By the way, another hack, suggested in a different forum, was to calculate the button size needs by using the button text's fontMetrics.boundingRect(). This outputs the amount of space that a given string (in a given font) will need. I would then hard code the button's maximumWidth to this value.
This allowed me to work around the problem, but again, doesn't explain what's going on under the hood with QPushButton

Thanks for your help! Much appreciated :)

wysota
4th December 2009, 20:09
There are no technical limitations. The significant differences between the two classes are that you can associate an action with a tool button, you can make it flat and that its minimum size is narrower than that of a push button because it is meant to be used on toolbars. Apart from that they are quite the same.