PDA

View Full Version : Same height for QComboBox and QPushButton



cevou
28th October 2008, 20:23
Hi,

I want a QComboBox to automatically get the height from a QPushButton which has default height.
When I try to read the buttons height with button->height() it returns something like 380, however it displays with a height of like 20.

How can I get the real height without setting the height of the button?

maverick_pol
28th October 2008, 20:53
Could you paste a small compilable example for us to test? A bit of code would help us to see the problem.

cevou
28th October 2008, 21:13
Ok, hope that helps you:


QPushButton *select = new QPushButton("auswählen");
cmb = new QComboBox;
cmb->setFixedHeight(select->geometry().height()); //select->geometry().height() returns 380 instead of the actual height


The PushButton's height is somehow defined by the OS and Layout.
In Windows Vista for example the standard height of the QComboBox is not equal to the standard height of a QPushButton. That's why I want to do all that stuff.

momesana
28th October 2008, 21:33
The widget must first be shown to have a valid size (height/width). Try to read the sizeHint instead though it may not be the actual size the widget gets once it is displayed. It will however come pretty close though to the size the widget will have once it is first shown.

cevou
28th October 2008, 21:40
Thanks! That worked!

monst
12th May 2012, 07:56
You can use stylesheet as a hack when you are close to pulling you hairs off:



comboBox->setStyleSheet("QComboBox { padding-top: 2px; padding-bottom: 1px; }");


It will correct ComboBox size.
You supply your own values (with QString::arg(), for instance).