Page 1 of 4 123 ... LastLast
Results 1 to 20 of 70

Thread: How to show progess in a QTreeView item ?

  1. #1
    Join Date
    May 2006
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to show progess in a QTreeView item ?

    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

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

    Default Re: How to show progess in a QTreeView item ?

    You could try using QTreeWidgetItem::setItemWidget and placing a QProgressBar widget in it.

  3. #3
    Join Date
    Jan 2006
    Location
    Cambridge, MA
    Posts
    32
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to show progess in a QTreeView item ?

    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:aint(..)
    {
    QStyleOptionProgressBarV2 opts;
    opts.palette = whatever;
    opts.rect = painter.viewport();
    opts.minimum = whatever
    opts.maximum = whatever
    opts.progress = index.model()->data(index,Qt:isplayRole).toInt();
    // etc
    // etc Remember to fill out everything in the options class hierarchy

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

    }

  4. #4
    Join Date
    May 2006
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to show progess in a QTreeView item ?

    Forgot my account password, couldn't post... Thanks wysota & Glitch. tanminh.

  5. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to show progess in a QTreeView item ?

    Quote Originally Posted by Glitch View Post
    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:aint(..)
    {
    QStyleOptionProgressBarV2 opts;
    opts.palette = whatever;
    opts.rect = painter.viewport();
    opts.minimum = whatever
    opts.maximum = whatever
    opts.progress = index.model()->data(index,Qt:isplayRole).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.

    Qt Code:
    1. /***************************************************************************
    2.  * Copyright (C) 2006 by Lawrence Lee *
    3.  * valheru@facticius.net *
    4.  * *
    5.  * This program is free software; you can redistribute it and/or modify *
    6.  * it under the terms of the GNU General Public License as published by *
    7.  * the Free Software Foundation; either version 2 of the License, or *
    8.  * (at your option) any later version. *
    9.  * *
    10.  * This program is distributed in the hope that it will be useful, *
    11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
    12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
    13.  * GNU General Public License for more details. *
    14.  * *
    15.  * You should have received a copy of the GNU General Public License *
    16.  * along with this program; if not, write to the *
    17.  * Free Software Foundation, Inc., *
    18.  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
    19.  ***************************************************************************/
    20. #include <QMenu>
    21. #include <QContextMenuEvent>
    22. #include <QAction>
    23. #include "queueList.h"
    24. #include "server.h"
    25. #include "queueListItemProgress.h"
    26.  
    27. QueueList::QueueList( Server *s, QWidget *parent ) : QTreeWidget( parent )
    28. {
    29. server = s;
    30.  
    31. ///Setup right click menu
    32. cancelDownload = new QAction( tr( "Cancel download" ), this );
    33. connect( cancelDownload, SIGNAL( triggered() ), this, SLOT( cancelDownloadSlot() ) );
    34. menu = new QMenu();
    35. menu->addAction( cancelDownload );
    36.  
    37. ///Define the layout and behaviour or the QTreeWidget
    38. setSelectionMode( QAbstractItemView::MultiSelection );
    39. setRootIsDecorated( false );
    40. setAlternatingRowColors( true );
    41. setHeaderLabels( ( QStringList ) "Job" << "Command" << "Status" << "Connection" << "Speed" );
    42. setColumnHidden( 1, true );
    43. setColumnHidden( 3, true );
    44.  
    45. ///Define the progress bar for the second visible column
    46. progressBar = new QueueListItemProgress();
    47. setItemDelegateForColumn( 2, progressBar );
    48. }
    49.  
    50. QueueList::~ QueueList()
    51. {
    52. delete progressBar;
    53. delete menu;
    54. delete cancelDownload;
    55. }
    56.  
    57. void QueueList::contextMenuEvent( QContextMenuEvent* event )
    58. {
    59. menu->popup( event->globalPos() );
    60. }
    61.  
    62. void QueueList::cancelDownloadSlot()
    63. {
    64. if( currentIndex().row() > -1 ){
    65. if ( currentItem()->text( 3 ) == "-1" ){ //Item isn't downloading yet, so we can simply delete it
    66. takeTopLevelItem( currentIndex().row() );
    67. }else{ //The item is being processed, so first we have to issue a quit command
    68. bool ok;
    69. server->connection( currentItem()->text( 3 ).toInt( &ok, 10 ) )->writeCommand( "QUIT\r\n" );
    70. currentItem()->setText( 3, "-1" );
    71. takeTopLevelItem( currentIndex().row() );
    72. }
    73. }
    74. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. /***************************************************************************
    2.  * Copyright (C) 2006 by Lawrence Lee *
    3.  * valheru@facticius.net *
    4.  * *
    5.  * This program is free software; you can redistribute it and/or modify *
    6.  * it under the terms of the GNU General Public License as published by *
    7.  * the Free Software Foundation; either version 2 of the License, or *
    8.  * (at your option) any later version. *
    9.  * *
    10.  * This program is distributed in the hope that it will be useful, *
    11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
    12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
    13.  * GNU General Public License for more details. *
    14.  * *
    15.  * You should have received a copy of the GNU General Public License *
    16.  * along with this program; if not, write to the *
    17.  * Free Software Foundation, Inc., *
    18.  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
    19.  ***************************************************************************/
    20. #include <QPainter>
    21. #include <QApplication>
    22. #include "queueListItemProgress.h"
    23.  
    24. QueueListItemProgress::QueueListItemProgress( QObject* parent ) : QItemDelegate( parent )
    25. {
    26. }
    27.  
    28. void QueueListItemProgress::paint( QPainter *painter, const QStyleOptionViewItem &option,
    29. const QModelIndex &index ) const
    30. {
    31. bar.maximum = 100;
    32. bar.progress = index.model()->data( index, Qt::DisplayRole ).toInt();
    33. QApplication::style()->drawControl( QStyle::CE_ProgressBar, &bar, painter, 0 );
    34. }
    To copy to clipboard, switch view to plain text mode 
    /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"
    Last edited by Valheru; 12th October 2006 at 19:25.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to show progess in a QTreeView item ?

    It's in the option.rect. Here's an example.
    J-P Nurmi

  7. #7
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to show progess in a QTreeView item ?

    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...

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to show progess in a QTreeView item ?

    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..
    Qt Code:
    1. setItemDelegate(progressBar);
    2.  
    3. void QueueListItemProgress::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    4. {
    5. if (index.column() == 2)
    6. {
    7. ...
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to show progess in a QTreeView item ?

    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

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to show progess in a QTreeView item ?

    Uh, where does it say so in the docs? Sure you can use a custom delegate with the convenience views.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class ItemDelegate: public QItemDelegate
    4. {
    5. public:
    6. ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
    7. {
    8. }
    9.  
    10. void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    11. {
    12. if (index.column() == 2)
    13. {
    14. int progress = (index.row() != 0 ? 100 / index.row() : 0);
    15.  
    16. // draw your cool progress bar here
    17. opt.rect = option.rect;
    18. opt.minimum = 0;
    19. opt.maximum = 100;
    20. opt.progress = progress;
    21. opt.text = QString("%1%").arg(progress);
    22. opt.textVisible = true;
    23. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0);
    24. }
    25. else
    26. {
    27. QItemDelegate::paint(painter, option, index);
    28. }
    29. }
    30. };
    31.  
    32. int main(int argc, char* argv[])
    33. {
    34. QApplication a(argc, argv);
    35.  
    36. tree.setWindowTitle("ProgressDelegate");
    37. tree.setHeaderLabels(QStringList() << "Job" << "Command" << "Status" << "Connection" << "Speed");
    38. for (int i = 0; i < 5; ++i)
    39. {
    40. new QTreeWidgetItem(&tree, QStringList() << "Job" << "Command" << "Status" << "Connection" << "Speed");
    41. }
    42. tree.setRootIsDecorated( false );
    43. tree.setAlternatingRowColors( true );
    44. tree.setItemDelegateForColumn(1, new ItemDelegate(&tree));
    45. tree.setColumnHidden( 1, true );
    46. tree.setColumnHidden( 3, true );
    47. tree.setItemDelegate(new ItemDelegate(&tree));
    48. tree.show();
    49. return a.exec();
    50. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    J-P Nurmi

  11. The following 4 users say thank you to jpn for this useful post:

    foxyproxy (3rd April 2008), ihope (11th August 2009), lvi (3rd September 2008), Valheru (13th October 2006)

  12. #11
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to show progess in a QTreeView item ?

    Quote Originally Posted by jpn View Post
    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 It works fine for QListViews. Not for QTreeWidgets though

    Quote Originally Posted by Qt4 docs
    void QTreeWidget::setItemWidget ( QTreeWidgetItem * item, int column, QWidget * 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 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 and subclass QItemDelegate instead.
    This function was introduced in Qt 4.1.
    See also itemWidget() and Delegate Classes.

  13. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to show progess in a QTreeView item ?

    Quote Originally Posted by Valheru View Post
    Don't worry, first thing that I did was compile that code you posted 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..
    J-P Nurmi

  14. #13
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to show progess in a QTreeView item ?

    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?

  15. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to show progess in a QTreeView item ?

    Quote Originally Posted by Valheru View Post
    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.

    Quote Originally Posted by Valheru View Post
    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:
    Quote Originally Posted by jpn View Post
    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.
    J-P Nurmi

  16. The following user says thank you to jpn for this useful post:

    Valheru (13th October 2006)

  17. #15
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to show progess in a QTreeView item ?

    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?
    Last edited by Valheru; 13th October 2006 at 07:49.

  18. #16
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to show progess in a QTreeView item ?

    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

  19. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to show progess in a QTreeView item ?

    There is no real progressbar. But you can access the value displayed using a line similar to this one:
    Qt Code:
    1. int progress = index.data(Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 
    In your case you might want to substitute index with item approach receiving:
    Qt Code:
    1. int progress = item->data(Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 

  20. #18
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to show progess in a QTreeView item ?

    Thank you for the response Wysota,

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

    Qt Code:
    1. item->setData(1,Qt::DisplayRole,updatingValue);
    To copy to clipboard, switch view to plain text mode 

    Bob

  21. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to show progess in a QTreeView item ?

    Yes, that's correct.

  22. #20
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to show progess in a QTreeView item ?

    Using the example from jpn I call

    Qt Code:
    1. tree.topLevelItem(1)->setData(1,Qt::DisplayRole,50);
    To copy to clipboard, switch view to plain text mode 

    and then

    Qt Code:
    1. qDebug() << tree.topLevelItem(1)->data(1,Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. Replies: 4
    Last Post: 26th September 2011, 13:02
  2. QTreeView and item editing
    By roxton in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 19:56
  3. QTreeView, QSortFilterProxyModel and item expansions
    By benacler in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2008, 21:30
  4. QTreeView: selection behavior upon selected item removal
    By Pieter from Belgium in forum Qt Programming
    Replies: 6
    Last Post: 11th July 2007, 17:00
  5. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 15:24

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.