Results 1 to 10 of 10

Thread: How to resize pushbutton to fit text?

  1. #1
    Join Date
    Jun 2011
    Posts
    19
    Thanks
    3

    Question How to resize pushbutton to fit text?

    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

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to resize pushbutton to fit text?

    You can use

    Qt Code:
    1. QSize QFontMetrics::size ( int flags, const QString & text, int tabStops = 0, int * tabArray = 0 ) const
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Aug 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to resize pushbutton to fit text?

    After you change the text of QPushButton, you can get it's new minimise size by minimumSize, then resize it by this size.

  4. #4
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to resize pushbutton to fit text?

    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.

  5. #5
    Join Date
    Jun 2011
    Posts
    19
    Thanks
    3

    Default Re: How to resize pushbutton to fit text?

    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

  6. #6
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to resize pushbutton to fit text?

    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
    Attached Files Attached Files

  7. The following user says thank you to MasterBLB for this useful post:

    nikhil (22nd June 2012)

  8. #7
    Join Date
    Jun 2011
    Posts
    19
    Thanks
    3

    Default Re: How to resize pushbutton to fit text?

    oh, so they only work if you put spacers? Because I did that and now it works

  9. #8
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to resize pushbutton to fit text?

    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.

  10. #9
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to resize pushbutton to fit text?

    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

  11. #10
    Join Date
    May 2012
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to resize pushbutton to fit text?

    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.

  12. The following user says thank you to jblackarty for this useful post:

    nikhil (22nd June 2012)

Similar Threads

  1. vertical pushbutton text
    By eric in forum Qt Programming
    Replies: 11
    Last Post: 18th April 2019, 19:17
  2. left alignment for pushbutton text
    By BalaQT in forum Qt Programming
    Replies: 7
    Last Post: 22nd September 2009, 08:41
  3. Wrap Text in PUSHBUTTON
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 13:54
  4. How to resize window with pushbutton
    By kaydknight in forum Newbie
    Replies: 2
    Last Post: 13th January 2007, 12:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.