Results 1 to 5 of 5

Thread: On subclassing QStandardItem and displaying different stuff

  1. #1
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11
    Wiki edits
    1

    Default On subclassing QStandardItem and displaying different stuff

    Hello.

    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
    A:40
    B:20

    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
    this->setText(label);
    THen I save the item in a QStandardItemModel. I display in a QListView and get the result
    A
    B

    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?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: On subclassing QStandardItem and displaying different stuff

    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.

  3. #3
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11
    Wiki edits
    1

    Default Re: On subclassing QStandardItem and displaying different stuff

    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?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: On subclassing QStandardItem and displaying different stuff

    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.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QTableView>
    3. #include <QStandardItemModel>
    4. #include <QList>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. QTableView view;
    10.  
    11.  
    12. model.setColumnCount( 2 );
    13.  
    14. QStandardItem * item;
    15. for ( int nItem = 0; nItem < 10; ++nItem )
    16. {
    17. QList< QStandardItem * > items;
    18.  
    19. items << new QStandardItem( QString( 'A' + nItem ) );
    20. items << new QStandardItem( QString::number( nItem * 10 + 10 ) );
    21. model.appendRow( items );
    22. }
    23.  
    24. view.setModel( &model );
    25. view.show();
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

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

    Table.png Tree.png
    Last edited by d_stranz; 20th November 2011 at 00:04.

  5. The following user says thank you to d_stranz for this useful post:

    Gunnar (20th November 2011)

  6. #5
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11
    Wiki edits
    1

    Default Re: On subclassing QStandardItem and displaying different stuff

    Thank you. That cleared out a few issues.

Similar Threads

  1. Phonon stuff
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2009, 12:26
  2. Get Hot New Stuff!!!!
    By Mystical Groovy in forum KDE Forum
    Replies: 4
    Last Post: 16th December 2008, 00:21
  3. adding PHONON and stuff
    By baray98 in forum Installation and Deployment
    Replies: 1
    Last Post: 18th August 2008, 17:37
  4. Subclassing QStandardItem
    By aLiNuSh in forum Newbie
    Replies: 4
    Last Post: 5th April 2007, 20:00
  5. Howto draw stuff
    By Morea in forum Newbie
    Replies: 16
    Last Post: 7th April 2006, 12:05

Tags for this Thread

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.