Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

No percentage label on QProgressBar when switching to vertical



  • I need to have a QProgressBar that is vertically aligned to show the visually the content of a solution container. When I align the QProgressBar horizontally I have the label visible that shows the percentage, when I align the QProgressBar vertically the label with the percentage is not visible anymore.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt ?
    On what platform ?

    Please provide a minimal compilable example that shows the behaviour.



  • Hi, thank you for your answer, it's QT 5.14.1 on Windows, is there a way to upload the *.ui-File in this forum?



  • @hkottmann
    No. Paste into a reply (preferably enclosed in Code tags), or you have to upload somewhere public and give a link.



  • Look like this is still this old unresolved bug:

    https://bugreports.qt.io/browse/QTBUG-31385?focusedCommentId=304710&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel

    The problem is the same in Windows and Linux, here the screenshot of a widget with a horizontal and a vertical QProgressBar with the same settings (except the orientation):

    ![alt text](qprgressbar.png progressbar.png)

    Here the code of the *.ui file:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>373</width>
        <height>465</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QGridLayout" name="gridLayout_2">
        <item row="0" column="0">
         <layout class="QGridLayout" name="gridLayout">
          <item row="0" column="0">
           <widget class="QProgressBar" name="progressBar">
            <property name="value">
             <number>24</number>
            </property>
           </widget>
          </item>
          <item row="0" column="1">
           <widget class="QProgressBar" name="progressBar_2">
            <property name="value">
             <number>24</number>
            </property>
            <property name="orientation">
             <enum>Qt::Vertical</enum>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>373</width>
         <height>30</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

  • Lifetime Qt Champion

    Hi
    Seems like the bug is still around
    Win 10. Qt 5.14.1

    alt text


  • Lifetime Qt Champion

    Since you have them, would either of you please update that report with a minimal compilable example ?


  • Lifetime Qt Champion

    @SGaist
    I added a comment with reference to this thread. ( with how to reproduce )
    Hope that is fine.


  • Lifetime Qt Champion

    Hi
    A fix could be using a QProxyStyle
    (fast made, give it more love if used for real)
    set with
    ui->progressBar->setStyle( new Progressproxy );

    class Progressproxy : public QProxyStyle
    {
    
    public:
        Progressproxy() : QProxyStyle() {}
    public:
        virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p,
                                 const QWidget *w) const override
        {
    
            if (element == CE_ProgressBarLabel) {
                const QStyleOptionProgressBar *real  = qstyleoption_cast<const QStyleOptionProgressBar *>(opt);
                // check real is not null...
                QStyleOptionProgressBar pb = *real;
                pb.rect = QRect(0, 0, w->width(), w->height());
                proxy()->drawItemText(p, pb.rect, Qt::AlignCenter, pb.palette,
                                      pb.state & State_Enabled, pb.text, QPalette::Text);
                return;
            }
    
            QProxyStyle::drawControl(element, opt, p, w);
    
        }
    };
    

    alt text

    But to have it reserve space in the button of the bar like on horizontal, it seems one has to handle
    all CE_ProgressBarXXX elements and reduce the rect (clientArea)
    as the space used for the text seems not included when vertical.
    (it uses the full widget area)
    alt text



  • Hi mrjj

    Nice hack, this enabled me also to display the percentage value as double value, thank you


  • Lifetime Qt Champion

    @hkottmann
    Hi
    Well, that sounds good as i was not sure having the value center would be ok since its normally at the end,
    and if you can tweak it for something you wanted, its a win.
    Also, its a small hack and while i didn't test it, it should also respond
    as expected on other properties (like disabled etc) since im
    using the normal draw functions.


Log in to reply