Results 1 to 12 of 12

Thread: How to display marquee animation of QProgressBar properly when using it in QTreeView

  1. #1
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Question How to display marquee animation of QProgressBar properly when using it in QTreeView

    Hi,

    I'm displaying QProgressBars in a QTreeView by subclassing QItemDelegate. Here's the code:
    Qt Code:
    1. void ProgressBarItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionProgressBar progressBarOption;
    4. progressBarOption.state = QStyle::State_Enabled;
    5. progressBarOption.direction = QApplication::layoutDirection();
    6. progressBarOption.rect = option.rect;
    7. progressBarOption.fontMetrics = QApplication::fontMetrics();
    8. progressBarOption.textAlignment = Qt::AlignCenter;
    9. progressBarOption.textVisible = true;
    10.  
    11. if(...)
    12. {...}
    13. else if(...)
    14. {...}
    15. else if(...)
    16. {
    17. progressBarOption.progress = 50;
    18. progressBarOption.minimum = 0;
    19. progressBarOption.maximum = 100;
    20. }
    21.  
    22. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
    23. }
    To copy to clipboard, switch view to plain text mode 

    As you can see I'm using the marquee animation of the progress bar (in some conditions) by setting minimum and maximimum to 0.
    I actually get the animation, but it doesn't look well because the progress bar isn't redrawn fast enough.
    Is there a way to redraw the progress bar fast enough to display the animation properly? Is it possible to tell the QItemDelegate to call paint() more frequent? Is it possible to display an QProgressBar directly, instead of calling QApplication::style()->drawControl(...)?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    Quote Originally Posted by Infinity View Post
    Is it possible to tell the QItemDelegate to call paint() more frequent?
    You can connect a timer's timeout() signal to the view's update slot() (possibly passing in the coordinates of the item containing the progress bar). But don't be suprised if CPU usage will go sky high.

    Is it possible to display an QProgressBar directly, instead of calling QApplication::style()->drawControl(...)?
    Yes but you will have to control its geometry yourself (including clipping and showing/hiding the widget). An alternative is to use setIndexWidget() but then the bar will not be tied to your model in any way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Infinity (9th August 2013)

  4. #3
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    I found another solution: I create a QProgressBar as editor display it via openPersistentEditor() permanently. It is not nice because the QProgressBar is a display widget and no editor, but it works without high CPU usage, without writing a lot of code and the QProgressBar is still tied to the model.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    Quote Originally Posted by Infinity View Post
    I found another solution: I create a QProgressBar as editor display it via openPersistentEditor() permanently. It is not nice because the QProgressBar is a display widget and no editor, but it works without high CPU usage, without writing a lot of code and the QProgressBar is still tied to the model.
    Did you test it on a model with more than a couple of rows?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    I'm already using persistent editors in another column in the QTreeView (QComboBoxes).

    Do you think that I have to expect performance problems?
    I've just tested my application with 218 rows (although I think more than 50 rows at the same time are already unrealistic). The CPU usage is a bit high I'm resizing the window or the columns but I think that's not a big problem. The memory usage is also ok.

    EDIT: I've tested the application again with 218 rows, but without the usage of persistent editors. The CPU usage is only a few percent lower and the memory usage only about 5 MB.
    Last edited by Infinity; 10th August 2013 at 18:27.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    Quote Originally Posted by Infinity View Post
    Do you think that I have to expect performance problems?
    Yes, you should expect performance problems. Especially if you run on Windows that constantly redraws progress bars.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    As already described, I don't notice a big difference between using persistent editors and not using them. But I tested the application only on Linux (using the QGtkStyle). I will test is on Windows later.

    that constantly redraws progress bars
    That's true, all progress bars (and not only the progress bars that are currently shown) will be constantly redrawn when I update all values constantly.
    Last edited by Infinity; 10th August 2013 at 21:35.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    Quote Originally Posted by Infinity View Post
    That's true, all progress bars (and not only the progress bars that are currently shown) will be constantly redrawn when I update all values constantly.
    There is no such thing as "constantly" in computers. Everything is discrete and updating bars "constantly" usually doesn't make much sense. The point here is that Vista+ draws the progress bar animated regardless if it changes its values or not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    You are right, "constantly" doesn't make sense.

    the progress bar animated regardless if it changes its values or not
    I haven't thought of that yet because I don't use Windows so often.

    Maybe I can reduce that problem by hiding progress bars that aren't visible. I will try it and test it on Windows.
    Last edited by Infinity; 10th August 2013 at 23:20.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    Quote Originally Posted by Infinity View Post
    I haven't thought of that yet because I don't use Windows so often.
    Neither do I but my students had to deal with the animated progress bar problem back in Qt 4.4-ish days. I don't think much has changed since then in this regard.

    Maybe I can reduce that problem by hiding progress bars that aren't visible. I will try it and test it on Windows.
    I think you should first think if you really need to update all those bars "constantly" as this seems to me like a bigger problem than Windows redrawing progress bars (you can always draw a progress bar yourself in the delegate to overcome this issue).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    I tested the application under Windows. There are no performance problems, but the CPU usage is higher. I think the animation (which I don't really need) is not worth it considering the higher CPU usage.

    As already mentioned I'm also displaying QComboBoxes in another column. Currently I'm using openPersistentEditor() to display them. Now I've tried to display them by manual painting:
    Qt Code:
    1. void ComboBoxItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const//(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionComboBox comboBoxOption;
    4. comboBoxOption.rect = option.rect;
    5. comboBoxOption.state = option.state;
    6. comboBoxOption.state |= QStyle::State_Enabled;
    7. comboBoxOption.state |= QStyle::State_On;
    8. comboBoxOption.subControls = QStyle::SC_All;
    9. comboBoxOption.editable = false;
    10. comboBoxOption.currentText = index.model()->data(index).toString();
    11.  
    12. QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
    13. QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
    14. }
    To copy to clipboard, switch view to plain text mode 

    On Windows the code works, but on Linux (when using QGtkStyle) I'm just getting the ComboBoxLabel. Does anybody know why painting the combo box not works with QGtkStyle?

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to display marquee animation of QProgressBar properly when using it in QTreeV

    Drawing a combobox is one thing. Making it work like a combobox is another. If you want to hear my personal opinion then pushing comboboxes into table columns is a violation against user experience If you consider what the column contains -- it contains some VALUE. The way you change the value and the sole fact that you can change the value (or not) is meaningless while displaying that value. If you display a bunch of comboboxes in a column, you just clutter the display and you have no functional benefit over just displaying the value contained in the field. If the user wants to edit a field, he will enter edit mode and use the combo that pops up, to modify the value which afterwards will again be displayed in the column.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 11
    Last Post: 30th March 2015, 06:03
  2. Replies: 0
    Last Post: 15th April 2011, 19:04
  3. QProgressBar "crash" - doesn't work properly
    By metRo_ in forum Qt Programming
    Replies: 6
    Last Post: 23rd October 2010, 13:56
  4. Replies: 21
    Last Post: 20th October 2010, 13:25
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.