PDA

View Full Version : QProgressBar as a QTreeView item



T4ng10r
22nd September 2011, 10:09
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.

sergio87
22nd September 2011, 10:48
I did a little class to show a QProgressBar:

ProgressDialog.h:

/*
* ProgressDialog.h
*
* Created on: 26/08/2011
* Author: Sergio Madrazo Giménez
*/

#include <QDialog>
#include <QProgressBar>
#include <QHBoxLayout>
#ifndef PROGRESSDIALOG_H_
#define PROGRESSDIALOG_H_

class ProgressDialog : public QDialog{

Q_OBJECT

public:
ProgressDialog(QWidget *parent=0, int t=0);
virtual ~ProgressDialog();

private:
int total;
QProgressBar *progressBar;

public slots:
void setNuber(int num);
};

#endif /* PROGRESSDIALOG_H_ */

ProgressDialog.cpp

/*
* ProgressDialog.cpp
*
* Created on: 26/08/2011
* Author: Sergio Madrazo Giménez
*/
#include <QtGui>
#include "ProgressDialog.h"

ProgressDialog::ProgressDialog(QWidget *parent, int t)
: QDialog(parent)
{
total = t;

progressBar = new QProgressBar (parent);
progressBar->setRange(0,100);
progressBar->setValue(50);
progressBar->setVisible(true);
progressBar->setMaximumSize(500,30);
progressBar->setMinimumSize(200,30);
}

ProgressDialog::~ProgressDialog() {
delete progressBar;
}


/********************************
************ SLOTS *************
********************************/

/*
* This slot changes the value of the progress bar.
* Input: number of items.
* It calculates automatically the percentage
*/
void ProgressDialog::setNuber(int num){
progressBar->setValue((num*100)/total);
}

This class has two paremeters imput:

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.
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! :p

T4ng10r
22nd September 2011, 11:17
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.

bender86
22nd September 2011, 11:25
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.

class ProgressDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ProgressDelegate(QObject *parent = 0)
: QStyledItemDelegate(parent)
{
}

void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
int total = 100;
// Assume your model just send the completed percentage.
// You may be creative here, I used this to send a QTime.
int progress = index.data(Qt::UserRole).toInt();

QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.minimum = 0;
progressBarOption.maximum = total;
progressBarOption.progress = progress;
progressBarOption.text = QString();
progressBarOption.textVisible = true;

QApplication::style()->drawControl(QStyle::CE_ProgressBar, & progressBarOption, painter);
}
}

@sergio87
What's wrong with QProgressDialog?

mentalmushroom
22nd September 2011, 11:27
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.

T4ng10r
22nd September 2011, 13:11
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.

mentalmushroom
22nd September 2011, 13:37
no, edit mode is not needed