title: Changing ToolButtonStyle of QToolButtons dynamically depending upon the size of window
body: <p>I have created a widget class (in <code>C++</code>) <code>ToolTray</code> which is basically few <code>QToolButton</code>s added in <code>QHboxLayout</code>. These buttons are used by the user for operations like <code>Save</code>, <code>Open</code> etc. Initially, the buttons are added with <code>toolButtonStyle</code> set to <code>Qt::ToolButtonTextBesideIcon</code>. </p>
  
  <p>I want to change the <code>toolButtonStyle</code> to <code>Qt::ToolButtonIconOnly</code> in <code>resizeEvent</code> if the new size is not sufficient to display text and icons of <code>QToolButton</code>. See the picture below:</p>
  
  <p><a href="https://i.stack.imgur.com/J7p2j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J7p2j.png" alt="enter image description here"></a></p>
  
  <p>If the window is resized and if the new size is sufficient to display the text and icon of all the QToolButton, then <code>toolButtonStyle</code> should be changed back to <code>Qt::ToolButtonTextBesideIcon</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/lCZRF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lCZRF.png" alt="enter image description here"></a></p>
  
  <p>I tried to achieve this with following code:</p>
  
  <pre><code>void ToolTray::resizeEvent(QResizeEvent *event)
  {
      int totalWidth = 0;
      bool mode = runBtn_-&gt;toolButtonStyle() == Qt::ToolButtonStyle::ToolButtonIconOnly;
      // Mode =&gt; True =&gt; For ICON Only
      // Mode =&gt; False =&gt; For ICON + Text
      for (auto btn: toolBtns_) {
          if (btn-&gt;isVisible())
              totalWidth += btn-&gt;size().width();
      }
  
      qDebug() &lt;&lt; "Total Width: " &lt;&lt; totalWidth ;
      qDebug() &lt;&lt; "Event Size: " &lt;&lt; event-&gt;size() &lt;&lt; " Old size " &lt;&lt; event-&gt;oldSize();
  
      if (event-&gt;oldSize().isEmpty()) // Ignore ResizeEvent for QSize(-1,-1)
          return;
  
      if (mode) { // Already Small
          if (event-&gt;size().width() &lt; preferedFullWidth_)
              return;
          for (auto btn: toolBtns_) {
              if (btn == moreBtn_)
                  continue;
              btn-&gt;setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
          }
          return;
      }
      // The QToolButtons are Text Beside Icon
      if (event-&gt;size().width() &gt;= totalWidth)
          return;
  
      qDebug() &lt;&lt; "Here";
      for (auto btn: toolBtns_)
          btn-&gt;setToolButtonStyle(Qt::ToolButtonIconOnly);
      preferedFullWidth_ = totalWidth;
  }
  </code></pre>
  
  <p>However, I am unable to achieve what I wanted:</p>
  
  <ol>
  <li><p>Whenever I try to shrink the size of the window, the QToolButton's Text first start clipping and after some more size reduction, the <code>ToolButtonStyle</code> is changed to <code>Qt::ToolButtonIconOnly</code>. I don't want clipping to happen.</p></li>
  <li><p>I also want to add some margin (or hysteresis) in my implementation. Like once the QToolButton is changed to <code>Qt::ToolButtonIconOnly</code> at a particular width, then the width should be greater than preferedFullWidth_ + certain margin to switch back to <code>Qt::ToolButtonTextBesideIcon</code>.</p></li>
  </ol>