Results 1 to 7 of 7

Thread: How to use standard model?

  1. #1
    Join Date
    Feb 2015
    Posts
    21
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How to use standard model?

    How to add data to standard model using data and not item.
    And please explain me who is the parent of the model and how to set them.

    I tried reading it from various source including Qt documentation but my concepts are not getting clear.
    So please do not tell me to google. I have already come here from googling.

    Thank You

  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 use standard model?

    Quote Originally Posted by freiza View Post
    How to add data to standard model using data and not item.
    Huh? You mean how to set the data using setData()? Hmm... use setData()

    And please explain me who is the parent of the model
    The object you set as the parent in the constructor or by calling setParent.

    and how to set them.
    QObject::setParent()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2015
    Posts
    21
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use standard model?

    suppose I want my model to have 2 rows and 3 column then how i should I set my Qstringlist?

  4. #4
    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 use standard model?

    What QStringList? I'm afraid you have to be more specific. QStringListModel is by definition one column only, of course you can override but then it's better to just use a model more tailored to your problem. However last time it seemed to me you were talking about QStandardItemModel.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2015
    Posts
    21
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to use standard model?

    Qt Code:
    1. void Dialog::createModel()
    2. {
    3. // qstr << "hello"<<"world"<<"this"<<"is"<<"cool"<<"qt";
    4. qstr.append("hello");
    5. qstr.append("world");
    6. qstr.append("this");
    7. qstr.append("is");
    8. qstr.append("cool");
    9. qstr.append("qt");
    10. booksModel = new QStandardItemModel();
    11. booksModel->insertRows(0,2);
    12. booksModel->insertColumns(0,3);
    13. int k=0;
    14. for(int i=0;i<booksModel->rowCount();i++)
    15. {
    16. for(int j=0;j<booksModel->columnCount();j++)
    17. {
    18. // QStandardItem *item = new QStandardItem(QString::number(j));
    19. QModelIndex index = booksModel->index(i,j,QModelIndex());
    20. //booksModel->setData(index,QVariant((i+1)*(j+1)));
    21. booksModel->setData(index,QVariant(qstr[k]));
    22. k++;
    23. }
    24. }
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 
    Question 1) Is there any way that I can pass "qstr" instead of "qstr[k]" and still it prints out everything correctly in tableview? I mean a structure that can be passed directly to set data such that I don't have to use for loop?
    Question 2) Instead of append can I populate qstr using "qstr << "hello"<<"world"<<"this"<<"is"<<"cool"<<"qt";"
    Question 3) How to use foreach loop instead of for loop in above case?

  6. #6
    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 use standard model?

    QStandardItemModel is meant to be used in such a way that you access elements as items so trying to work around it means you probably shouldn't be using that class. Regarding your questions:
    1. Not using setData and pure standard item model. You can subclass the model and introduce a method which will accept a list and distribute its content among the items.
    2. Yes
    3. You would have to track current values of i and j yourself which would make the loop quite complicated.

    Now towards a solution - if what you want is a model which will distribute its data in a tabular way among a specified number of columns then it's best to subclass QAbstractTableModel and implement a custom model which will do just that. Internally you can store the data in a stringlist and append to that list when new data arrives. Knowing the number of columns you can use values of row and column from the model index to calculate which item in the list the index corresponds to. The whole class should be less than 20 lines of code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to use standard model?

    Quote Originally Posted by freiza View Post
    Question 1) Is there any way that I can pass "qstr" instead of "qstr[k]" and still it prints out everything correctly in tableview? I mean a structure that can be passed directly to set data such that I don't have to use for loop?
    Question 2) Instead of append can I populate qstr using "qstr << "hello"<<"world"<<"this"<<"is"<<"cool"<<"qt";"
    Question 3) How to use foreach loop instead of for loop in above case?
    1) No, not if you use QStandardItemModel. If you subclass QAbstractItemModel, you can implement the model using whatever container type you desire.

    2) Yes, of course.

    3) See below:

    Qt Code:
    1. int i = 0;
    2.  
    3. foreach (str, qstr)
    4. {
    5. int row = i / model.columnCount();
    6. int col = i % model.columnCount();
    7. QModelIndex index = model.index(row, col);
    8. model.setData(index, str);
    9. i++;
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 29th August 2013, 06:41
  2. Replies: 0
    Last Post: 14th October 2011, 22:50
  3. Replies: 0
    Last Post: 28th July 2011, 06:22
  4. Choice between Standard- and Abstract Item Model
    By hunsrus in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2009, 16:14
  5. Replies: 2
    Last Post: 2nd June 2008, 09:45

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.