Results 1 to 7 of 7

Thread: QProgressBar as a QTreeView item

  1. #1
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QProgressBar as a QTreeView item

    Hi,
    I would like to use QProgressBar to show progress of some work. Then at the end of progress change item to text. How to do it in most simple way?
    What comes to my mind is - subclass QTreeView to my own View (a little to complex) or use persistent QProgressBar delegate.

  2. The following user says thank you to T4ng10r for this useful post:


  3. #2
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Lightbulb Re: QProgressBar as a QTreeView item

    I did a little class to show a QProgressBar:

    ProgressDialog.h:
    Qt Code:
    1. /*
    2.  * ProgressDialog.h
    3.  *
    4.  * Created on: 26/08/2011
    5.  * Author: Sergio Madrazo Giménez
    6.  */
    7.  
    8. #include <QDialog>
    9. #include <QProgressBar>
    10. #include <QHBoxLayout>
    11. #ifndef PROGRESSDIALOG_H_
    12. #define PROGRESSDIALOG_H_
    13.  
    14. class ProgressDialog : public QDialog{
    15.  
    16. Q_OBJECT
    17.  
    18. public:
    19. ProgressDialog(QWidget *parent=0, int t=0);
    20. virtual ~ProgressDialog();
    21.  
    22. private:
    23. int total;
    24. QProgressBar *progressBar;
    25.  
    26. public slots:
    27. void setNuber(int num);
    28. };
    29.  
    30. #endif /* PROGRESSDIALOG_H_ */
    To copy to clipboard, switch view to plain text mode 
    ProgressDialog.cpp
    Qt Code:
    1. /*
    2.  * ProgressDialog.cpp
    3.  *
    4.  * Created on: 26/08/2011
    5.  * Author: Sergio Madrazo Giménez
    6.  */
    7. #include <QtGui>
    8. #include "ProgressDialog.h"
    9.  
    10. ProgressDialog::ProgressDialog(QWidget *parent, int t)
    11. : QDialog(parent)
    12. {
    13. total = t;
    14.  
    15. progressBar = new QProgressBar (parent);
    16. progressBar->setRange(0,100);
    17. progressBar->setValue(50);
    18. progressBar->setVisible(true);
    19. progressBar->setMaximumSize(500,30);
    20. progressBar->setMinimumSize(200,30);
    21. }
    22.  
    23. ProgressDialog::~ProgressDialog() {
    24. delete progressBar;
    25. }
    26.  
    27.  
    28. /********************************
    29.  ************ SLOTS *************
    30.  ********************************/
    31.  
    32. /*
    33.  * This slot changes the value of the progress bar.
    34.  * Input: number of items.
    35.  * It calculates automatically the percentage
    36.  */
    37. void ProgressDialog::setNuber(int num){
    38. progressBar->setValue((num*100)/total);
    39. }
    To copy to clipboard, switch view to plain text mode 

    This class has two paremeters imput:
    1. QWidget *parent: When you call the constructor you can put it the Widget that you want. If you put 0, the it will open a new window.
    2. int t: this is the total. For example, if you have 5 things to do, it will be 5. Every time you finish one thing you call the method setImage with the number you finished.


    Sorry for my poor english!

  4. The following user says thank you to sergio87 for this useful post:


  5. #3
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProgressBar as a QTreeView item

    Thank you for you effort, but you completely misunderstood what I meant. I want to use QTreeView with some first level values. Then under each of them I need to add from one to three items to display progress of realization of subtask.
    So I need to work a way to place QProgressBar as a item in QTreeView row.

  6. The following user says thank you to T4ng10r for this useful post:


  7. #4
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProgressBar as a QTreeView item

    I couldn't find any more the source for this solution, but you can use an item delegate. This is just a sample code, but you should be able to figure out how to adapt it.
    Qt Code:
    1. class ProgressDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ProgressDelegate(QObject *parent = 0)
    6. : QStyledItemDelegate(parent)
    7. {
    8. }
    9.  
    10. void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    11. {
    12. int total = 100;
    13. // Assume your model just send the completed percentage.
    14. // You may be creative here, I used this to send a QTime.
    15. int progress = index.data(Qt::UserRole).toInt();
    16.  
    17. QStyleOptionProgressBar progressBarOption;
    18. progressBarOption.rect = option.rect;
    19. progressBarOption.minimum = 0;
    20. progressBarOption.maximum = total;
    21. progressBarOption.progress = progress;
    22. progressBarOption.text = QString();
    23. progressBarOption.textVisible = true;
    24.  
    25. QApplication::style()->drawControl(QStyle::CE_ProgressBar, & progressBarOption, painter);
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    @sergio87
    What's wrong with QProgressDialog?

  8. The following user says thank you to bender86 for this useful post:


  9. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QProgressBar as a QTreeView item

    Subclass QStyledItemDelegate, override its paint method, use QStyle::drawControl along with QStyle::CE_ProgressBar to draw progress bar, set this delegate for some column of your tree view.

  10. The following user says thank you to mentalmushroom for this useful post:


  11. #6
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProgressBar as a QTreeView item

    Correct me if I'm wrong, but using ItemDelegates with View require to set item in edit mode. I know that I can force it by using PersistentEditor on items. From my previous experience - opening PersistentEditor slows down GUI.
    I understand that this is the simplest solution and as this I will try yours code samples.

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


  13. #7
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QProgressBar as a QTreeView item

    no, edit mode is not needed

  14. The following user says thank you to mentalmushroom for this useful post:


Similar Threads

  1. QTreeView Checkable Item
    By fruzzo in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2011, 10:41
  2. Extract Item from the QTreeView
    By sajis997 in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2011, 08:40
  3. Add Item in QDIRMODEL + QTREEVIEW
    By kamlmish in forum Newbie
    Replies: 0
    Last Post: 7th January 2011, 08:35
  4. Buttons on item in QTreeView?
    By joeld42 in forum Qt Programming
    Replies: 1
    Last Post: 9th October 2010, 01:27
  5. QTreeView and item editing
    By roxton in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 18:56

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.