View Full Version : On subclassing QStandardItem and displaying different stuff

19th November 2011, 15:28

I'm trying to write a simple timetracker, and perhaps I'm doing this the wrong way, so I must ask for advice (since I'm not really comfortable yet with the modell/view programming).

In my program, I enter a label for a task and start/stop a timer. Then I want to store each time interval for each unique task label and display the total running time for each unique label.
For example: the labels and times
A: 10
B: 20
A: 30

should be displayd with

To do this, I have a class Task that is a subclass of QStandardItem and I save the time intervals in a QList and sets the name of the task by using
THen I save the item in a QStandardItemModel. I display in a QListView and get the result

but no times. So, what should I do to display the total time next to the label string? Can I use a special role? How? I'm stuck here.

The way I then add another label and time, for example A: 14, I search for the item in the model with the label A and add the value 14 to the QList and calculate the sum for label A.

Any suggestions?

19th November 2011, 19:24
If you want to display two or more separate things for each QStandardItem, then each one needs to be in its own column, and you need to set the columnCount() for the item to the right value. In your case, columnCount == 2 and column 0 contains the task name, column 1 contains the task time.

19th November 2011, 20:34
Thanks for your reply, but I think I need a little more detail.
Can I use QListView for this? (two items on the same row?)

And when I add a item with the label A, should I create a child item to that item, and set the runnig time to it?

19th November 2011, 20:46
Here's an example. One thing to note, which may be why you are having a problem in the first place: QListView only supports *one* column. If you want more than one column, you need to use QTableView or QTreeView.

#include <QtGui/QApplication>
#include <QTableView>
#include <QStandardItemModel>
#include <QList>

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

QStandardItemModel model;

model.setColumnCount( 2 );

QStandardItem * item;
for ( int nItem = 0; nItem < 10; ++nItem )
QList< QStandardItem * > items;

items << new QStandardItem( QString( 'A' + nItem ) );
items << new QStandardItem( QString::number( nItem * 10 + 10 ) );
model.appendRow( items );

view.setModel( &model );
return a.exec();

The attached screnshots show table and tree views; all I did was change the view type from QTableView to QTreeView.

7117 7116

20th November 2011, 07:30
Thank you. That cleared out a few issues.