Originally Posted by
Glitch
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.
/***************************************************************************
* 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"
{
server = s;
///Setup right click menu
cancelDownload
= new QAction( tr
( "Cancel download" ),
this );
connect( cancelDownload, SIGNAL( triggered() ), this, SLOT( cancelDownloadSlot() ) );
menu->addAction( cancelDownload );
///Define the layout and behaviour or the QTreeWidget
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;
}
{
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 <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() );
}
}
}
To copy to clipboard, switch view to plain text mode
/***************************************************************************
* 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"
{
}
{
bar.maximum = 100;
bar.progress = index.model()->data( index, Qt::DisplayRole ).toInt();
}
/***************************************************************************
* 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 );
}
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"
Bookmarks