Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to size QPushButtons to width of text and pack left?
Forum Updated to NodeBB v4.3 + New Features

How to size QPushButtons to width of text and pack left?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnby
    wrote on last edited by johnby
    #1

    I have two buttons on a QVBoxLayout and am trying to make the buttons the width of their text and not strech across the complete QVBoxLayout. Should have both buttons packed to the left.

    I've tried the following to no avail.

    auto *button1 = new QPushButton(QLatin1String("b1"), parent);
    auto *button2 = new QPushButton(QLatin1String("b2"), parent);
    
    auto s1 = button1->fontMetrics().size(Qt::TextShowMnemonic, button1->text());
    auto s2 = button2->fontMetrics().size(Qt::TextShowMnemonic, button2->text());
        
    QStyleOptionButton opt1;
    opt1.initFrom(button1);
    opt1.rect.setSize(s1);
    
    QStyleOptionButton opt2;
    opt2.initFrom(button2);
    opt2.rect.setSize(s2);
    
    button1->setMinimumSize(button1->style()->sizeFromContents(
        QStyle::CT_PushButton, &opt1, textSize, button1));
    
    button2->setMinimumSize(button2->style()->sizeFromContents(
        QStyle::CT_PushButton, &opt2, textSize, button2));
    
    QHBoxLayout *controlsLayout = new QHBoxLayout;
    controlsLayout->addWidget(button1);
    controlsLayout->addWidget(button2);
    
    QVBoxLayout *renderLayout = new QVBoxLayout;
    renderLayout->addLayout(renderControlsLayout);
    renderLayout->setContentsMargins(0, 0, 0, 0);
    

    Anyone have thoughts?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      ???What are you doing?
      Setting size in a layout is meaningless, they will be resized by the layout in anytime.
      You should use layout functions to control their size, instead of calculating by yourself.

      auto *button1 = new QPushButton(QLatin1String("b1"), parent);
      button1->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
      auto *button2 = new QPushButton(QLatin1String("b2"), parent);
      button2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
      
      QVBoxLayout *renderLayout = new QVBoxLayout;
      renderLayout->addWidget(button1, 0, Qt::AlignLeft);
      renderLayout->addWidget(button2, 0, Qt::AlignLeft);
      

      Here's the problem, by default QPushButton has a minimum size hint of about (80, 20) in all kinds of styles.
      So if your text is not long enough, it will probably use 80 as the width.
      If you need it to be shorter, I prefer using QToolButton instead, it doesn't have such minimum value.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To ensure they are pushed to the left, add a stretch after the last button.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved