Results 1 to 12 of 12

Thread: How to store data in a tablemodel

  1. #1
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default How to store data in a tablemodel

    Hello.

    I'm new to model view programming but I've successfully implemented a model inherited from QAbstractListModel:

    Qt Code:
    1. class PaletteListModel : public QAbstractListModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit PaletteListModel(QList<QColor> colors, QObject *parent);
    7. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    8. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    9. ...
    10. ...
    11. ...
    12. private:
    13. QList<QColor> m_colors;
    14.  
    15. };
    16.  
    17. PaletteListModel::PaletteListModel(QList<QColor> colors, QObject *parent) :
    18. {
    19. m_colors = colors;
    20. }
    21.  
    22. int PaletteListModel::rowCount(const QModelIndex & ) const
    23. {
    24. return m_colors.count();
    25. }
    26. ...
    27. ...
    To copy to clipboard, switch view to plain text mode 

    So I can pass a list which can have a dynamic length to the constructor which will be stored in a private field. With this I can determine the row count...

    But how can I pass and store the data for a table model? I guess it has to be some kind of nested list. But I can't figure out how exactly to do this:

    Qt Code:
    1. class PaletteTableModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit PaletteTableModel(??????? colors, QObject *parent);
    7. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    8. int columnCount(const QModelIndex &parent = QModelIndex())) const;
    9.  
    10. private:
    11. ???????? m_colors;
    12.  
    13. };
    14.  
    15.  
    16. PaletteTableModel::PaletteTableModel(??????? colors, QObject *parent) :
    17. {
    18. m_colors = colors;
    19. }
    20.  
    21. int PaletteTableModel::rowCount(const QModelIndex & ) const
    22. {
    23. return ?????
    24. }
    25.  
    26. int PaletteTableModel::columnCount(const QModelIndex &) const
    27. {
    28. return ?????
    29. }
    30. ...
    31. ...
    32. ...
    To copy to clipboard, switch view to plain text mode 

    How do I have to implement this?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to store data in a tablemodel

    Not sure what you need. Can you clarify your thoughts?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: How to store data in a tablemodel

    I want to have a model which stores a table of colors.

    So basically: What do I have to write for the question marks in the second Qt Code of my first post?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to store data in a tablemodel

    Well, you already have the list of private colors -- it is the storage of the model.
    You need to implement QAbstractItemModel::data (to display your colors), QAbstractItemModel::setData (to change existing colors), QAbstractItemModel::insertRows (to insert new colors), QAbstractItemModel::removeRows (to remove colors).

    This is good stuff for reading.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: How to store data in a tablemodel

    Quote Originally Posted by spirit View Post
    Well, you already have the list of private colors -- it is the storage of the model.
    As I have written, implementing a list works fine. Now I want to implement a table not a list.

    And I don't know how to store and pass the the two dimensional data through the constructor. For the one dimensional data I used QList<QColor> as I have written in the first Qt Code of my first posting.

    But what 'datatype' or 'datastructure' do I have to use for an two dimensional data? Marked with question marks in the second Qt Code of my first post.


    Quote Originally Posted by spirit View Post
    You need to implement QAbstractItemModel::data (to display your colors), QAbstractItemModel::setData (to change existing colors), QAbstractItemModel::insertRows (to insert new colors), QAbstractItemModel::removeRows (to remove colors).

    This is good stuff for reading.
    I'm aware of that and already know how to do that. What I don't know is how to pass two dimensional data through the constructor, store the two dimensional data and determine the range of my two dimensional data in rowCount(const QModelIndex & ) and columnCount(const QModelIndex &) const.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to store data in a tablemodel

    Okay, got it.
    Yes, you should use nested lists or vectors in this case.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: How to store data in a tablemodel

    Okay, thank you. But how do I have to do it? Could you complete the second Qt code for my first post, so I can see how to do it?

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to store data in a tablemodel

    Qt Code:
    1. class PaletteTableModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit PaletteTableModel(const QList<QList<QColor> > &colors, QObject *parent);
    7. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    8. int columnCount(const QModelIndex &parent = QModelIndex())) const;
    9.  
    10. private:
    11. QList<QList<QColor> > m_colors; //suppose the first list keeps rows, the second -- columns
    12.  
    13. };
    14.  
    15.  
    16. PaletteTableModel::PaletteTableModel(const QList<QList<QColor> > &colors, QObject *parent) :
    17. QAbstractTableModel(parent), m_colors(colors)
    18. {
    19.  
    20. }
    21.  
    22. int PaletteTableModel::rowCount(const QModelIndex &) const
    23. {
    24. return m_colors.count();
    25. }
    26.  
    27. int PaletteTableModel::columnCount(const QModelIndex &index) const
    28. {
    29. //TODO: add an index checks and boundary checks
    30. return m_colors.at(index.row()).count();
    31. }
    32. ...
    33. ...
    34. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: How to store data in a tablemodel

    Thank you for your completion. I have tried to complete the code:

    Qt Code:
    1. PaletteTableModel::PaletteTableModel(QList< QList<QColor> > colors, QObject *parent) :
    2. {
    3. m_colors = colors;
    4. }
    5.  
    6. int PaletteTableModel::rowCount(const QModelIndex & ) const
    7. {
    8. return m_colors.count();
    9. }
    10.  
    11. int PaletteTableModel::columnCount(const QModelIndex &index) const
    12. {
    13. return m_colors.at(index.row()).count();
    14. }
    To copy to clipboard, switch view to plain text mode 

    For the first try I passed an empty list to the constructor:

    Qt Code:
    1. private:
    2. PaletteTableModel *m_model;
    3. QList< QList<QColor> > m_list;
    4.  
    5. ...
    6. ...
    7.  
    8. m_model = new PaletteTableModel(m_list, this);
    9. ui->tableView->setModel(m_model);
    To copy to clipboard, switch view to plain text mode 

    But setModel fails with
    ---------------------------
    Microsoft Visual C++ Runtime Library
    ---------------------------
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    ---------------------------
    OK
    ---------------------------
    Where is the problem?

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to store data in a tablemodel

    I think the problem is here
    Qt Code:
    1. int PaletteTableModel::columnCount(const QModelIndex &index) const
    2. {
    3. return m_colors.at(index.row()).count();
    4. }
    To copy to clipboard, switch view to plain text mode 
    You are not checking the index. That leads to error in QList::at -- wrong range.
    I specially added TODO in this method definition.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: How to store data in a tablemodel

    I'm not able to implement a check...

    The first thing I've tried is this
    Qt Code:
    1. int PaletteTableModel::columnCount(const QModelIndex &index) const
    2. {
    3. if(index.row() > (m_colors.count() - 1))
    4. return 0;
    5.  
    6. if(index.column() > (m_colors.at(index.row()).count()) - 1)
    7. return 0;
    8.  
    9. return m_colors.at(index.row()).count();
    10. }
    To copy to clipboard, switch view to plain text mode 

    But the function is getting called with index.row() = -1. Why gets the function called with index.row() = -1?
    So I extended the function
    Qt Code:
    1. int PaletteTableModel::columnCount(const QModelIndex &index) const
    2. {
    3. if(index.row() < 0)
    4. return 0;
    5.  
    6. if(index.row() > (m_colors.count() - 1))
    7. return 0;
    8.  
    9. if(index.column() > (m_colors.at(index.row()).count()) - 1)
    10. return 0;
    11.  
    12. return m_colors.at(index.row()).count();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Now it works with an empty list.
    But when I try to fill the two dimensional list and pass it like this
    Qt Code:
    1. QList<QColor> list;
    2.  
    3. list << QColor(Qt::red) << QColor(Qt::green) << QColor(Qt::blue);
    4. m_list << list << list << list;
    5.  
    6. m_model = new PaletteTableModel(m_list, this);
    To copy to clipboard, switch view to plain text mode 

    I still get the same error. What am I doing wrong?

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to store data in a tablemodel

    Quote Originally Posted by rubikon View Post
    But the function is getting called with index.row() = -1. Why gets the function called with index.row() = -1?
    I still get the same error. What am I doing wrong?
    Because an index can be invalid see QModelIndex::isValid.
    So, you should at first check the index validity
    Qt Code:
    1. ...
    2. if (!index.isValid())
    3. return 0;
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. How to store huge data in a Qt software
    By Momergil in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2012, 23:43
  2. Need ideas on how to store data in SQL-Database
    By homerun4711 in forum Newbie
    Replies: 3
    Last Post: 6th January 2011, 00:10
  3. Data from TableModel and into Text Edit
    By Nefastious in forum Newbie
    Replies: 1
    Last Post: 16th September 2009, 03:36
  4. Replies: 5
    Last Post: 9th April 2008, 00:10
  5. How to store/get pointer on QTreeWidgetItem data?
    By Teerayoot in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2007, 23:26

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.