PDA

View Full Version : How to resize pushbutton to fit text?



Dreamerzz
11th August 2011, 19:53
I am designing a button which may be re translated in many languages.

How can i get the button to automatically re-size to fit all of the text ?
ex) Lets say my button barely fits the word "accept". If i re-translate in German, the word "akzeptieren" wouldn't fit and some of the letters cannot be visible.

How do I automatically have the push button re-size to do this?

Thanks

qlands
12th August 2011, 09:30
You can use



QSize QFontMetrics::size ( int flags, const QString & text, int tabStops = 0, int * tabArray = 0 ) const


and then setGeometry to the button using the size

You can create QFontMetrics with a special font if you are translating to arabic for example.

However because of the change in size, it is recommended to have widgets inside layouts so they will expand while taking other widgets into consideration.

If you are making an application to handle multiple languages, consider to read the documentation about Internationalization.

cplus
12th August 2011, 09:36
After you change the text of QPushButton, you can get it's new minimise size by minimumSize, then resize it by this size. :)

MasterBLB
12th August 2011, 10:44
There is no need to bother calculations of size of the button.It's just enough to put the button into a layout,set it's horizontal resize policy to Minimum-from now if the button text changes to longer it'll be resized automatically.

Dreamerzz
12th August 2011, 14:35
MasterBLB your suggestion did not work, even after i set the size policy to minimum expanding or minimum, and I am not sure what you mean by putting it into its layout, set "its" horizontal resize policy. I dont know what "it" is that you are referring to, the frame or the button.

cplus i tried

ui->pushButton_3->resize(ui->pushButton_3->minimumSize());

but all that did is make the button disappear

qlands I have no idea what I am supposed to do with all that haha. can you please provide some context?

Thanks everyone

can someone provide some sample code? like 2 buttons where pressing one changes the text size of the other so you can see it autoadjust

MasterBLB
12th August 2011, 14:50
So you've made something wrong-see yourself

EDIT:
Ahhhh,you do not know what the layout is...Well,you'll have to come back to Qt learning and develop programs after you'll find out

Dreamerzz
12th August 2011, 14:55
oh, so they only work if you put spacers? Because I did that and now it works

MasterBLB
12th August 2011, 15:34
I put the spacers because without them the button is resized to take whole width the main windows provides.But the key factor is that QMainWindow has a grid layout set on.

Zanjy
17th October 2011, 17:04
OK, it's not as easy as it seems. I'm facing the same Problem. I do something like this:

QHBoxLayout* some_layout = new QHBoxLayout();
some_lay->setMargin(0);
some_lay->setSpacing(0);
some_lay->setContentsMargins(0, 0, 0, 0);
some_lay->setSizeConstraint(QLayout::SetMinAndMaxSize);
setLayout(some_lay);

QWidget* left_tabs = new QWidget(this);
left_tabs->setMinimumHeight(40);
left_tabs->setMaximumHeight(40);
left_tabs->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
some_lay->addWidget(left_tabs);

QHBoxLayout* left_tab_lay = new QHBoxLayout();
left_tab_lay->setMargin(0);
left_tab_lay->setSpacing(0);
left_tab_lay->setContentsMargins(0, 0, 0, 0);
left_tab_lay->setSizeConstraint(QLayout::SetMinAndMaxSize);
left_tab_lay->setAlignment(Qt::AlignBottom | Qt::AlignVCenter);
left_tabs->setLayout(left_tab_lay);

QWidget* display_wid = new QWidget(this);
display_wid->setMinimumHeight(40);
display_wid->setMaximumHeight(40);
display_wid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
some_lay->addWidget(display_wid);

QPushButton* newtab = new QPushButton(tr("props"), this);
newtab->setCheckable(true);
newtab->setFixedHeight(40);
newtab->setMaximumWidth(200);
newtab->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
left_tab_lay->addWidget(newtab);

QPushButton* nexttab = new QPushButton(tr("needs"), this);
nexttab->setCheckable(true);
nexttab->setFixedHeight(40);
nexttab->setMaximumWidth(200);
nexttab->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
left_tab_lay->addWidget(nexttab);

"props" will be translated with "Eigenschaften" and "needs" with "Bedingungen" which both are longer than the original. My Problem is, that buttons does not resize after changing the language, no matter what. I tried everything: updateGeometry(), resize to minimum size adjustSize(), nothing worked. So what am I doing wrong? It seems the sizeHint is not recalculated properly

jblackarty
16th May 2012, 13:01
I'm also faced this problem. I searched over all internet and tried all possible combinations. Nothing helped. Such topics usually end with no solution or with words like MasterBLB said: "It must work automatically !" But solution proposed by MasterBLB works, oddly enough. I did some experiments trying to reproduce it and found two partial solutions:
1. Place at least one spacer on preferred side of button in layout. (Either spacers on both sides for central alignment.) It will give space for growing but limits with parent widget boundaries finally.
2. Place button directly in layout of topmost-level widget (i.e. QMainWindow or QDialog). Or you can place it in layout which belongs to nested layouts up to the topmost-level widget. That widget must be resizeable, of course.
I have no explanations for them :) These are best I found until now. If somebody have better ones, please share your knowledge. :)