PDA

View Full Version : How to show progess in a QTreeView item ?



tanminh
20th May 2006, 23:30
Hi,

Has anybody written code to show or paint progress in a QTreeWidgetItem ? If so, would you please kindly share it ?

Thanks a lot.
tanminh

wysota
21st May 2006, 00:06
You could try using QTreeWidgetItem::setItemWidget and placing a QProgressBar widget in it.

Glitch
23rd May 2006, 14:26
setItemWidget(..) will work well for small number of rows. For something more flexable you might want to make a QItemDelegate that will instead of drawing the number 1 if it is an integer in the model draw a progress bar. Some inspirational code follows that uses the Qt Styling mechanism. There will be no widget allocations to make 50,000 of these in a table if you wish :) Code is for inspiration only, it obviously does not compile ;)

MyDelegate::paint(..)
{
QStyleOptionProgressBarV2 opts;
opts.palette = whatever;
opts.rect = painter.viewport();
opts.minimum = whatever
opts.maximum = whatever
opts.progress = index.model()->data(index,Qt::DisplayRole).toInt();
// etc
// etc Remember to fill out everything in the options class hierarchy

QApplication::style()->drawControl(QStyle::CE_ProgressBar, opts, painter, 0);

}

tanminh
3rd June 2006, 00:19
Forgot my account password, couldn't post... Thanks wysota & Glitch. tanminh.

Valheru
12th October 2006, 18:08
setItemWidget(..) will work well for small number of rows. For something more flexable you might want to make a QItemDelegate that will instead of drawing the number 1 if it is an integer in the model draw a progress bar. Some inspirational code follows that uses the Qt Styling mechanism. There will be no widget allocations to make 50,000 of these in a table if you wish. Code is for inspiration only, it obviously does not compile.

MyDelegate::paint(..)
{
QStyleOptionProgressBarV2 opts;
opts.palette = whatever;
opts.rect = painter.viewport();
opts.minimum = whatever
opts.maximum = whatever
opts.progress = index.model()->data(index,Qt::DisplayRole).toInt();
// etc
// etc Remember to fill out everything in the options class hierarchy

QApplication::style()->drawControl(QStyle::CE_ProgressBar, opts, painter, 0);

}

I have tried to implement what you describe here. I want to paint the progress bar in the second column of a subclassed QTreeWidget. I am unsure as to how to add the subclassed QItemDelegate to my subclassed QTreeWidget. I think I am doing it right, as I am adding it using the setItemDelegateForColumn() function, but no progress bar shows up, only the numbers I am changing in the QTreeView as time goes by.


/************************************************** *************************
* Copyright (C) 2006 by Lawrence Lee *
* valheru@facticius.net *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
************************************************** *************************/
#include <QMenu>
#include <QContextMenuEvent>
#include <QAction>
#include "queueList.h"
#include "server.h"
#include "queueListItemProgress.h"

QueueList::QueueList( Server *s, QWidget *parent ) : QTreeWidget( parent )
{
server = s;

///Setup right click menu
cancelDownload = new QAction( tr( "Cancel download" ), this );
connect( cancelDownload, SIGNAL( triggered() ), this, SLOT( cancelDownloadSlot() ) );
menu = new QMenu();
menu->addAction( cancelDownload );

///Define the layout and behaviour or the QTreeWidget
setSelectionMode( QAbstractItemView::MultiSelection );
setRootIsDecorated( false );
setAlternatingRowColors( true );
setHeaderLabels( ( QStringList ) "Job" << "Command" << "Status" << "Connection" << "Speed" );
setColumnHidden( 1, true );
setColumnHidden( 3, true );

///Define the progress bar for the second visible column
progressBar = new QueueListItemProgress();
setItemDelegateForColumn( 2, progressBar );
}

QueueList::~ QueueList()
{
delete progressBar;
delete menu;
delete cancelDownload;
}

void QueueList::contextMenuEvent( QContextMenuEvent* event )
{
menu->popup( event->globalPos() );
}

void QueueList::cancelDownloadSlot()
{
if( currentIndex().row() > -1 ){
if ( currentItem()->text( 3 ) == "-1" ){ //Item isn't downloading yet, so we can simply delete it
takeTopLevelItem( currentIndex().row() );
}else{ //The item is being processed, so first we have to issue a quit command
bool ok;
server->connection( currentItem()->text( 3 ).toInt( &ok, 10 ) )->writeCommand( "QUIT\r\n" );
currentItem()->setText( 3, "-1" );
takeTopLevelItem( currentIndex().row() );
}
}
}

/************************************************** *************************
* Copyright (C) 2006 by Lawrence Lee *
* valheru@facticius.net *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
************************************************** *************************/
#include <QPainter>
#include <QApplication>
#include "queueListItemProgress.h"

QueueListItemProgress::QueueListItemProgress( QObject* parent ) : QItemDelegate( parent )
{
}

void QueueListItemProgress::paint( QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index ) const
{
QStyleOptionProgressBarV2 bar;
bar.maximum = 100;
bar.progress = index.model()->data( index, Qt::DisplayRole ).toInt();
QApplication::style()->drawControl( QStyle::CE_ProgressBar, &bar, painter, 0 );
}
/edit : hmm, using qDebug() inside funtions I can see that the overridden paint() function is never being called...why would that be?
I think it may have to do wit hthe size, since the docs say:


When reimplementing this function in a subclass, you should update the area held by the option's rect variable, using the option's state variable to determine the state of the item to be displayed, and adjust the way it is painted accordingly.

But I can't figure out a way to obtain the size of the QModelIndex it's "in" :(

jpn
12th October 2006, 19:35
It's in the option.rect. Here's an example (http://qtnode.net/pastebin/762).

Valheru
12th October 2006, 20:55
Thanks for the reply. Unfortunately, that's not fixing it. I'd put in a qDebug() line in both the constructor and the paint() function - the one in the constructor is getting dumped to the console, but the one in paint() never is. It's not getting called, and I don't understand it. Do I need to set something else, like a displayrole? It's just a standard QTreeWidget - I subclassed it so I could implement right mouse clicks. I also add QTreeWidgetItems into it for the objects. That's it. There are three columns that are visible in it, and two that are not. Could this be killing it? I don't see how - if something was being rendered to the hidden columns, paint would still get called...

jpn
12th October 2006, 21:36
Looks like an item delegate for row or column is not used for painting. Don't ask me why. I just took a look at the sources and I can't see itemDelegateForColumn() or itemDelegateForRow() being used in any of QAbstractItemView's subclasses...

So for now, you will have to do it the good old way.. :)


setItemDelegate(progressBar);

void QueueListItemProgress::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.column() == 2)
{
...
}
}

Valheru
12th October 2006, 21:37
Hmm, after staring bleary-eyed at the docs it appears that this isn't going to work. I'm going to have to use a QTreeView to get the QItemDelegate to work. And to do that, I shall have to subclass a QAbstractItemModel to use as the model for the QTreeView. Sorry for bothering you with this, it's in the docs. Blame it on a lack of sleep and 12 hours straight programming :p

jpn
12th October 2006, 21:49
Uh, where does it say so in the docs? Sure you can use a custom delegate with the convenience views.



#include <QtGui>

class ItemDelegate: public QItemDelegate
{
public:
ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
{
}

void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.column() == 2)
{
int progress = (index.row() != 0 ? 100 / index.row() : 0);

// draw your cool progress bar here
QStyleOptionProgressBar opt;
opt.rect = option.rect;
opt.minimum = 0;
opt.maximum = 100;
opt.progress = progress;
opt.text = QString("%1%").arg(progress);
opt.textVisible = true;
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0);
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);

QTreeWidget tree;
tree.setWindowTitle("ProgressDelegate");
tree.setHeaderLabels(QStringList() << "Job" << "Command" << "Status" << "Connection" << "Speed");
for (int i = 0; i < 5; ++i)
{
new QTreeWidgetItem(&tree, QStringList() << "Job" << "Command" << "Status" << "Connection" << "Speed");
}
tree.setRootIsDecorated( false );
tree.setAlternatingRowColors( true );
tree.setItemDelegateForColumn(1, new ItemDelegate(&tree));
tree.setColumnHidden( 1, true );
tree.setColumnHidden( 3, true );
tree.setItemDelegate(new ItemDelegate(&tree));
tree.show();
return a.exec();
}

Valheru
12th October 2006, 21:55
Uh, where does it say so in the docs? Sure you can use a custom delegate with the convenience views.

Don't worry, first thing that I did was compile that code you posted :p It works fine for QListViews. Not for QTreeWidgets though :(



void QTreeWidget::setItemWidget ( QTreeWidgetItem (http://doc.trolltech.com/4.2/qtreewidgetitem.html) * item, int column, QWidget (http://doc.trolltech.com/4.2/qwidget.html) * widget )

Sets the given widget to be displayed in the cell specified by the given item and column.
Note that the given widget's autoFillBackground (http://doc.trolltech.com/4.2/qwidget.html) property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the tree widget item.
This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView (http://doc.trolltech.com/4.2/qtreeview.html) and subclass QItemDelegate (http://doc.trolltech.com/4.2/qitemdelegate.html) instead.
This function was introduced in Qt 4.1.
See also itemWidget (http://doc.trolltech.com/4.2/qtreewidget.html#itemWidget)() and Delegate Classes (http://doc.trolltech.com/4.2/model-view-delegate.html).

jpn
12th October 2006, 22:04
Don't worry, first thing that I did was compile that code you posted :p It works fine for QListViews. Not for QTreeWidgets though :(
Are you drunk? :) There is a QTreeWidget in the example code of my previous post. The quotation of docs state that you should use delegates for drawing dynamic content instead of item widgets. That's exactly what is being done in the example..

Valheru
12th October 2006, 22:45
Wait, now I'm thoughroughly confused. I would have thought that a progress bar counts as dynamic content?

And the qDebug() call that I had in my code was the first line in the paint() function ... it wasn't being communicated to the console. Why not?

jpn
12th October 2006, 22:55
Wait, now I'm thoughroughly confused. I would have thought that a progress bar counts as dynamic content?
Yes, and it is painted by a delegate isn't it? The progress bar is not set as an item widget. It is painted by the delegate using the help of Qstyle.



And the qDebug() call that I had in my code was the first line in the paint() function ... it wasn't being communicated to the console. Why not?

As I tried to explain in one of my earlier posts:

Looks like an item delegate for row or column is not used for painting. Don't ask me why. I just took a look at the sources and I can't see itemDelegateForColumn() or itemDelegateForRow() being used in any of QAbstractItemView's subclasses...
There are new functions in Qt 4.2 called QAbstractItemView::setItemDelegateForRow() and QAbstractItemView::setItemDelegateForColumn(). For some reason, these column/row specific delegates are not used for painting (maybe they will be used in future releases, who knows?). That's why your paint() method was not getting called, because it was set as a column specific delegate. If you set it as the whole view's delegate, the paint() will get called, just like in the example. You will just have to check the column by hand and draw the progress bar only if appropriate column in question.

Valheru
13th October 2006, 06:29
Ok, thank you very much for the explanation :) I understand it now. It was a bit confusing that the method setItemDelegateForColumn wasn't working, that made me think that the QTreeWidget couldn't do what I wanted.

One last question on the matter : for some actions it is never known at what point they are, percentage-wise. How could you make the item delegate draw a progress bar similar to the one used at Windows startup?

coderbob
9th October 2007, 15:47
This might seem like a silly question, but in this example how would I access each progressbar for each individual treewidgetitem using the convenience view QTreeWidget?

Bob

wysota
9th October 2007, 15:55
There is no real progressbar. But you can access the value displayed using a line similar to this one:

int progress = index.data(Qt::DisplayRole).toInt();
In your case you might want to substitute index with item approach receiving:

int progress = item->data(Qt::DisplayRole).toInt();

coderbob
9th October 2007, 17:03
Thank you for the response Wysota,

So if I wanted to update it continuously with progress I would need to access it with?


item->setData(1,Qt::DisplayRole,updatingValue);

Bob

wysota
9th October 2007, 17:12
Yes, that's correct.

coderbob
9th October 2007, 17:37
Using the example from jpn I call


tree.topLevelItem(1)->setData(1,Qt::DisplayRole,50);

and then


qDebug() << tree.topLevelItem(1)->data(1,Qt::DisplayRole).toInt();

to confirm the value has changed but I do not get an update in the GUI displayed progress column. Do I need to also call a repaint or some other event?

Bob

jpn
9th October 2007, 19:17
The example generates dummy progress values. Substitute

int progress = (index.row() != 0 ? 100 / index.row() : 0);
with

int progress = index.data(Qt::DisplayRole).toInt();
in ItemDelegate::paint().

coderbob
10th October 2007, 03:24
Thank you Wysota and jpn,

One last question on this subject, in jpn's example the maximum size is set


opt.maximum = 100;

Is there a way to set a different maximum for each item?

Bob

jpn
10th October 2007, 07:27
You could use for example custom roles. Qt::UserRole is the first role that can be used for application-specific purposes:


// define custom roles
const int MinimumRole = Qt::UserRole;
const int MaximumRole = MinimumRole + 1;

// set data for custom roles
item->setData(column, MinimumRole, minimum);
item->setData(column, MaximumRole, maximum);

// get data for custom roles
opt.minimum = index.data(MinimumRole).toInt();
opt.maximum = index.data(MaximumRole).toInt();

wysota
10th October 2007, 09:52
You could even add a ValueRole and then use DisplayRole for something else (like displaying some text).

coderbob
13th October 2007, 18:55
Thank you for the help it has been invaluable.

Bob

fruzzo
28th December 2007, 18:21
the examples in this 3D are about Qt4, is it? there is a way to do the same thing (a progress bar in a colum of a QListView) for Qt 3.3.5?

marcel
28th December 2007, 18:27
Yes, the thread is about Qt 4.
In Qt 3 you cannot do it like that. You can instead subclass QListViewItem and paint the progress yourself.
See Q3ListViewItem::paintCell()

fruzzo
28th December 2007, 18:55
Yes, the thread is about Qt 4.
In Qt 3 you cannot do it like that. You can instead subclass QListViewItem and paint the progress yourself.
See Q3ListViewItem::paintCell()

ok thanxs...do you know if there are some examples about this?

marcel
28th December 2007, 19:01
Well, no examples that I know of, but you can play with it.
Basically you could keep the progress value in your item subclass and just consider it when doing the repainting. It shouldn't be that hard - you have the painter, the width and height of the cell... Just paint a rectangle with the width of progressValue*cellWidth/100 in the cell.

fruzzo
1st January 2008, 17:19
Well, no examples that I know of, but you can play with it.
Basically you could keep the progress value in your item subclass and just consider it when doing the repainting. It shouldn't be that hard - you have the painter, the width and height of the cell... Just paint a rectangle with the width of progressValue*cellWidth/100 in the cell.


well...I modified the paincell for myListyViewItem to create a progress bar like I want.
now I want update the progress bar for each row of the list every 1 second.
Any idea to do this ask???
I thought to create for each item a qtimer that every second update ther percent of the progress and call the paintcell but I don't know how call well the paintcell function.

marcel
1st January 2008, 17:23
I thought to create for each item a qtimer that every second update ther percent of the progress a call the paintcell but I don't know how call well the paintcell function.

That's not really good. Why not create a single timer, on whose timeout() you will update all items.
You can call either one of the Q3ListView::updateContents() functions or Q3ListView::repaintItem() for all items.

fruzzo
1st January 2008, 23:52
That's not really good. Why not create a single timer, on whose timeout() you will update all items.
You can call either one of the Q3ListView::updateContents() functions or Q3ListView::repaintItem() for all items.

ok ... I try...and now that I have a myListViewItem how can I iterate the listview to modify each item? The QLIstViewItemIterator is seem to don't work.

marcel
2nd January 2008, 00:45
ok ... I try...and now that I have a myListViewItem how can I iterate the listview to modify each item? The QLIstViewItemIterator is seem to don't work.
How come it doesn't work?
This is the example from Assistant:


QList<Q3ListViewItem *> lst;
Q3ListViewItemIterator it(myListView);
while (it.current()) {
if (it.current()->isSelected())
lst.append(it.current());
++it;
}

So, it pretty much should work.

THRESHE
27th February 2008, 16:58
It appears that style sheets can not be used with QItemDelegate. Is it so ? If it is how can I apply styles to my widgets ?
Thanks

jpn
27th February 2008, 18:51
It appears that style sheets can not be used with QItemDelegate. Is it so ?
That's right, unfortunately. Luckily Qt 4.4 will introduce QStyledItemDelegate (http://doc.trolltech.com/4.4beta/qstyleditemdelegate.html).

THRESHE
27th February 2008, 18:58
But QStyle can be used for styling or not ? And if it can is it much harder to use QStyle instead style sheets ? Thanks

jpn
27th February 2008, 19:14
But QStyle can be used for styling or not ? And if it can is it much harder to use QStyle instead style sheets ?
Sure it can. Well, writing a custom QStyle subclass is same as painting stuff by hand whereas style sheets are way more abstract way to style applications. I suggest you take a sneak peak to src/gui/styles/qxxxstyle.cpp what it looks like to implement a custom style. :)

THRESHE
28th February 2008, 12:38
I suggest you take a sneak peak to src/gui/styles/qxxxstyle.cpp what it looks like to implement a custom style. :)
It's not so easy :( Could you point me to some easier example ?

wysota
28th February 2008, 13:21
It's not so easy :(

That's the point of him redirecting you there :) There is an easier example bundled with Qt, but it only does minor changes.

THRESHE
28th February 2008, 13:32
That's the point of him redirecting you there :) There is an easier example bundled with Qt, but it only does minor changes.
If that's easy then I should think about giving up styling my app :(

wysota
28th February 2008, 13:45
No, you just shouldn't implement a custom style only to make very small changes if those changes can be made using simpler methods. Especially that QItemDelegate doesn't use the style directly, so you will be doing double work. I suggest you just implement a delegate and do your custom drawing there with or without using QStyle.

THRESHE
28th February 2008, 13:55
Any examples how to do that ? :o

wysota
28th February 2008, 14:13
Search the forum. There are numerous threads about delegates here.

THRESHE
29th February 2008, 17:53
And how is ItemDelegate repainted by force ?

wysota
29th February 2008, 18:39
What do you mean? The view calls the delegate when it needs to update some part of its viewport. If you want to force a redraw, just update() the viewport (whole or part of it).

THRESHE
1st March 2008, 13:47
Where do I get viewport to update ?

wysota
1st March 2008, 16:10
From the view :)

THRESHE
1st March 2008, 16:16
Is it so hard to answer more widely? :eek:

If I do so will it repaint my delegate ?


QTableWidget* tableWidget = new QTableWidget(this);
ItemDelegate delegate = new ItemDelegate(tableWidget);
tableWidget->setItemDelegateForColumn(4, delegate);
.................................................. ........
.................................................. ........
tableWidget->update();

Thanks in advance

wysota
1st March 2008, 16:29
No, it won't repaint your delegate, it will repaint your view. The delegate can't be "repainted", it is nothing visual, it's strictly a helper object that does some work on behalf of the view.

THRESHE
1st March 2008, 16:42
Ok :) Will it repaint my progressbar ? Or will it call ItemDelegate:: paint() ?

wysota
1st March 2008, 16:46
It depends :) If nothing in the item changed, the view might not call the delegate (especially if the item is currently not visible), but in general case - yes, it will call your delegate's paint method.

THRESHE
1st March 2008, 16:50
In my case it doesn't want to update :(

Could you suggest any other solutions ?

wysota
1st March 2008, 16:59
What exactly is your usecase? What exactly did you do and what do you want to achieve?

THRESHE
1st March 2008, 17:05
The thing is that I have a fancy progressbar that changes its colour depending on the state of download and when I start download the color doesn't change
It looks like on the first screenshot though it should look like on the second :confused:

wysota
1st March 2008, 17:14
Do you update the values in the items?

THRESHE
1st March 2008, 17:15
Do you update the values in the items?
What values and where ? Did not understand at all :confused:

wysota
1st March 2008, 18:49
Where do you keep the progress then?

THRESHE
1st March 2008, 21:27
I keep all needed values in my class that handles downloading called DownloadHolder :)

wysota
1st March 2008, 22:09
What's the point of having the view then? You should really keep those in the model or in the items. That's the whole point of having the view in the first place. Oh... and your delegate would work correctly. I suggest you read this whole thread again, it probably has some code to show you how to get a more or less complete solution.

THRESHE
1st March 2008, 23:05
What's the point of having the view then?
The purpose is displaying progress not handling logics

wysota
2nd March 2008, 08:10
You are effectively doubling your structures.

http://blog.wysota.eu.org/index.php/2007/12/17/itemviews-data-redundancy/

The progress is part of your data structure thus the datastructure should be informing the environment through a model (or modifying a set of items, if you insist on using the convenience approach) about every change that happens to it so that the view can update itself properly. How come do you expect the delegate to update the progress if it doesn't know anything changed?

THRESHE
2nd March 2008, 08:36
The progress is part of your data structure thus the datastructure should be informing the environment through a model (or modifying a set of items, if you insist on using the convenience approach) about every change that happens to it so that the view can update itself properly
How can I do that ?


How come do you expect the delegate to update the progress if it doesn't know anything changed?
I wanted to explicitly upadte it something like ItemDelegate->update() and that I could call it from anywhere in my app

wysota
2nd March 2008, 08:45
Imagine the viewport of the view as a sheet of paper you can draw on. Furthermore imagine you have a set of stamps that you can modify (i.e. change numbers to set a date). The stamp is your delegate - the view can set its attributes and use it to stamp all items in needs on the sheet of paper. The stamp doesn't do anything by itself, it can't be "updated" because a single stamp during a single pass can be used to draw an arbitrary number of different items (the stamp has no state). The view (you) in turn knows how to prepare the stamp because it knows the idea of how the final image should look like - your imagination of the image is the "model". You need to change your imagination to detect that you need to draw something differently on the paper, right? And that's exactly what you need to do - update the model to reflect all new changes.

You are using the so called convenience widgets which have an internal model and allow manipulation of particular cells through a set of item pointers. So to change the model, you need to change properties of those items - QTreeWidgetItem::setText or QTreeWidgetItem::setData might be good places to start doing some reading.

THRESHE
3rd March 2008, 11:30
wysota I really appreciate that you are trying to help me but it would be much better if you backup your words with code :)

For instance if I do like this


class QProgressDelegate : public QItemDelegate
{
Q_OBJECT

public:
QProgressDelegate(QObject *parent=0);
~QProgressDelegate();

void paint (QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
void NewProgress(int p)
{
progress = p;
}

private:
int progress;
};

Will calling NewProgress() repaint my progress bar ?

wysota
3rd March 2008, 11:42
No, it won't.


class Dele : public QItemDelegate {
public:
void paint(QPainter *p, const .... &opt, const QModelIndex &ind const {
int val = ind.data(Qt::DisplayRole).toInt();
QRect rect = opt.rect;
rect.setWidth(rect.width()*val/100);
p->drawRect(rect);
}
};

QTreeWidget Item *item = ....

item->setText(1, 40);
// view will be updated here
item->setText(1, 60);
// view will be updated here

You have at least two almost complete solution on the very first page of this thread. Didn't you see them?

THRESHE
3rd March 2008, 12:14
Thanks that is what I was asking you :)

THRESHE
3rd March 2008, 17:01
And one more question I'd like to ask. I don't know if this is the right topic so don't blame me too much ;)

The problem is when I click on a row in my table widget it becomes highlighted but when I use delegates it is not. I've tried to do like this


if (table->currentRow() == index.row())
{
painter->setBrush(QBrush(QColor("#444444")));
painter->drawRect(opt.rect);
}

but it didn't work the way I thought. Also I've tried to use setBackground with QPainter and that didn't work either :(

jpn
3rd March 2008, 17:05
The QStyleOptionViewItem parameter contains all required information.


if (opt.state & QStyle::State_Selected)
...

if (opt.state & QStyle::State_HasFocus)
...

etc.

THRESHE
3rd March 2008, 17:17
The QStyleOptionViewItem parameter contains all required information.


if (opt.state & QStyle::State_Selected)
...

if (opt.state & QStyle::State_HasFocus)
...

etc.
And what after ???
I've tried like


if (opt.state & QStyle::State_Selected)
{
painter->setBrush(QBrush(QColor("#444444")));
painter->drawRect(opt.rect);
}

Result -

THRESHE
3rd March 2008, 17:55
Sorry it was something with my brushes now it works fine :o