Results 1 to 3 of 3

Thread: QAbstractItemModel, QStandardItemModel

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Question QAbstractItemModel, QStandardItemModel

    Hello!

    I am new to Qt and hope you can help me. I am trying to write a simple application for invoice and customer management.

    The situation is:

    Header-File:
    Qt Code:
    1. ...
    2. ...
    To copy to clipboard, switch view to plain text mode 

    CPP-File:
    Qt Code:
    1. ...
    2. model = new QStandardItemModel(modelSizeRow, modelSizeColumn, this);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    Now I was trying to populate a table with

    Qt Code:
    1. model->setData(model->index(row, counter, QModelIndex()), pieces.value(counter));
    To copy to clipboard, switch view to plain text mode 

    So far everything works fine. But after some reading I thought that populating the model with QStandardItem might be a better idea. (Is this right?)

    Qt Code:
    1. QStandardItem *item = new QStandardItem(QString(pieces.value(counter)));
    2. model->setItem(row, counter, item);
    To copy to clipboard, switch view to plain text mode 

    This does not work, I get the following message.

    class QAbstractItemModel has no member named setItem
    Can someone tell me why? I am confused by the following code:
    Qt Code:
    1. ...
    2. ...
    3. model = new QStandardItemModel(modelSizeRow, modelSizeColumn, this);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    What happens if a type is set to a different type using the keyword new? Is this possible if the objects are related by inheritance?

    Thanks in advance!
    Kind regards,
    HomeR

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QAbstractItemModel, QStandardItemModel

    Quote Originally Posted by homerun4711 View Post
    Qt Code:
    1. model->setData(model->index(row, counter, QModelIndex()), pieces.value(counter));
    To copy to clipboard, switch view to plain text mode 
    While this might work if you created enough indexes when creating the model, technically, this is not the correct way to add items.
    In fact, the items are already there, but this will not work when you need to add data to an item with an index that doesn't exist.

    But after some reading I thought that populating the model with QStandardItem might be a better idea. (Is this right?)
    In the case of a QStanderItemModel, then yes, use a QStandardItem.

    Qt Code:
    1. QStandardItem *item = new QStandardItem(QString(pieces.value(counter)));
    2. model->setItem(row, counter, item);
    To copy to clipboard, switch view to plain text mode 
    This does not work,
    As explained above, if row and counter point to a place with no index, this will not work.
    Let the model add the indexes for you and use appendItem or insertItem

    I am confused by the following code:
    Qt Code:
    1. ...
    2. ...
    3. model = new QStandardItemModel(modelSizeRow, modelSizeColumn, this);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    What happens if a type is set to a different type using the keyword new? Is this possible if the objects are related by inheritance?
    Since a QStandarItemModel inherits a QAbstractItemModel, this is allowed, but it is not correct.
    Why do you want to define the model as a QAbstractItemModel and not as a QStandardItemModel?

  3. The following user says thank you to tbscope for this useful post:

    homerun4711 (16th December 2010)

  4. #3
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: QAbstractItemModel, QStandardItemModel

    Thanks for your answer, this cleared some things for me.

    Why do you want to define the model as a QAbstractItemModel and not as a QStandardItemModel?
    Well, this was not my idea I grabbed some code from Qt's build-in examples, this one was from itemviews/chart/mainwindow.h and mainwindow.cpp.

    Let the model add the indexes for you and use appendItem or insertItem.
    This is done with QModelIndex(), right? I have to read about that first. Does a model assign an QModelIndex to its items automatically or do I have to create one?

    Kind regards,
    HomeR

Similar Threads

  1. Best practices concerning QStandardItemModel
    By tim47 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2010, 05:29
  2. QStandardItemModel Help
    By frenk_castle in forum Newbie
    Replies: 1
    Last Post: 16th January 2010, 17:54
  3. QStandardItemModel alignment
    By Pembar in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2009, 16:07
  4. Print QStandardItemModel
    By Mrdata in forum Newbie
    Replies: 1
    Last Post: 4th July 2007, 23:14
  5. QTreeView with QStandardItemModel
    By steg90 in forum Newbie
    Replies: 3
    Last Post: 16th May 2007, 09:28

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.