Results 1 to 20 of 20

Thread: Help with getting my custom model working

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Quote Originally Posted by fullmetalcoder View Post
    Also, I believe that what you're trying to do is having a collection of connectionData structure inside your model, each one representing a row. Is that true? In this case you must not do it as you tried but instead setup an underlying data structure (e.g. a list of connection data) without using setData() and using them to feed the view through data()...
    Yes, I was going for a list of items stored in a QList and each item in the QList represents an item in my ListView. This is what I currently have:
    Qt Code:
    1. class ConnectionListModel : public QAbstractListModel
    2. {
    3. Q_OBJECT
    4.  
    5. enum sessionTypesEnum {
    6. secureShell,
    7. remoteFileBrowser
    8. };
    9.  
    10. struct connectionData {
    11. QString name;
    12. QString hostAddress;
    13. QString username;
    14. QString password;
    15. int port;
    16. sessionTypesEnum sessionType;
    17. };
    18.  
    19. public:
    20. ConnectionListModel(QObject *parent = 0);
    21.  
    22. ~ConnectionListModel();
    23.  
    24. //Required items to make this model work
    25. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    26. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    27.  
    28. //required items to be able to edit items
    29. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    30. Qt::ItemFlags flags(const QModelIndex &index) const;
    31.  
    32.  
    33. //Required to be able to resize the model (add & remove shit)
    34. bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
    35. bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
    36.  
    37. private:
    38. QList<connectionData> connectionList;
    39. };
    40.  
    41. ConnectionListModel::ConnectionListModel(QObject *parent)
    42. {
    43. }
    44.  
    45.  
    46. ConnectionListModel::~ConnectionListModel()
    47. {
    48. }
    49.  
    50. int ConnectionListModel::rowCount(const QModelIndex &parent) const
    51. {
    52. return connectionList.count();
    53. }
    54.  
    55. Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
    56. {
    57. if (!index.isValid())
    58. return Qt::ItemIsEnabled;
    59.  
    60. return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    61. }
    62.  
    63. QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
    64. {
    65. if ( ! index.isValid())
    66. return QVariant();
    67.  
    68. int row = index.row();
    69. if ( row < 0 || row >= connectionList.count() ) //Is the row invalid?
    70. return QVariant();
    71.  
    72. if (role == Qt::DisplayRole)
    73. {
    74. switch( index.column() ){
    75. case 0: return connectionList[row].name;
    76. case 1: return connectionList[row].hostAddress;
    77. case 2: return connectionList[row].username;
    78. case 3: return connectionList[row].password;
    79. case 4: return connectionList[row].port;
    80. case 5: return connectionList[row].sessionType;
    81. return QVariant();
    82. }
    83. }
    84. else
    85. return QVariant();
    86. }
    87.  
    88. bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
    89. {
    90. if (index.isValid() && role == Qt::DisplayRole)
    91. {
    92. int row = index.row();
    93. //Lets cast our QVariant so we can get the data out
    94.  
    95. const connectionData *newData = qvariant_cast<connectionData>( value );
    96.  
    97. //get a pointer to the list item so we can change its contents
    98. connectionList[row].name = newData.name;
    99. connectionList[row].hostAddress = newData.hostAddress;
    100. connectionList[row].username = newData.username;
    101. connectionList[row].password = newData.password;
    102. connectionList[row].port = newData.port;
    103. connectionList[row].sessionType = newData.sessionType;
    104.  
    105. emit dataChanged(index, index);
    106. return true;
    107. }
    108. return false;
    109. }
    110.  
    111.  
    112. bool ConnectionListModel::insertRows(int row, int count, const QModelIndex &parent)
    113. {
    114. beginInsertRows(parent, row, row +count-1);
    115. //Not finished!
    116. endInsertRows();
    117. }
    118.  
    119. bool ConnectionListModel::removeRows(int row, int count, const QModelIndex &parent)
    120. {
    121. beginRemoveRows(parent, row, row+count+1);
    122. //Not finished!
    123. endRemoveRows();
    124. }
    To copy to clipboard, switch view to plain text mode 
    The problem that I have though is that the qvariant_cast<connectionData> doesn't work. I'm not really sure how to pass in the connectionData object using a QVariant.

    Paul

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Quote Originally Posted by thomaspu View Post
    The problem that I have though is that the qvariant_cast<connectionData> doesn't work. I'm not really sure how to pass in the connectionData object using a QVariant.
    As fullmetalcoder wrote, use Q_DECLARE_METATYPE.

  3. #3
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    OK, I declared my custom datatype outside the class and now that seems to work so that I can set items that are in my QList<connectionData> list.
    Qt Code:
    1. class ConnectionListModel : public QAbstractListModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. struct connectionData {
    7. QString name;
    8. QString hostAddress;
    9. QString username;
    10. QString password;
    11. int port;
    12. sessionTypesEnum sessionType;
    13. };
    14.  
    15. ConnectionListModel(QObject *parent = 0);
    16. ~ConnectionListModel();
    17.  
    18. private:
    19. QList<connectionData> connectionList;
    20. };
    21. Q_DECLARE_METATYPE(ConnectionListModel::connectionData);
    To copy to clipboard, switch view to plain text mode 
    But now I'm lost as to how to add data to it. I understand that I have to do my own insertRows function. Is the purpose of the insertRows function just to insert a row, but not add the data to it?

    In other words, to insert a row, I'd be expected to do something like this to the model:
    Qt Code:
    1. conenctionData addMe;
    2. //set items in addMe
    3. myModel.insertRows(1, 1, QModelIndex ????); // <-what do i do here?
    4. myModel.setData(QModelIndex ???, addMe);
    To copy to clipboard, switch view to plain text mode 

    Paul

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting my custom model working

    Quote Originally Posted by thomaspu View Post
    myModel.insertRows(1, 1, QModelIndex ????); // <-what do i do here?
    You can use the default value for the last parameter (i.e. QModelIndex()). Your items don't have a parent, since you have a flat list. If you had a tree, then you would have to specify it to add items below the root.

    Quote Originally Posted by thomaspu View Post
    myModel.setData(QModelIndex ???, addMe);
    Here you need a model index that points to that empty row, you have added in previous line. You can get it using the index() method:
    Qt Code:
    1. myModel.setData( myModel.index( 0 ), addMe);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting my custom model working

    I'm making slow progress here. Understanding this stuff is frustrating. Now I can put stuff in the model (I think) but it doesn't show up in my associated view. I think I've got the setData() dataChanged signal all messed up. It keeps giving me comipler errors.

    Qt Code:
    1. bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. if (index.isValid() && role == Qt::DisplayRole)
    4. {
    5. int row = index.row();
    6. //Lets cast our QVariant so we can get the data out
    7.  
    8. connectionData newData = value.value<connectionData>();
    9.  
    10. //get a pointer to the list item so we can change its contents
    11. connectionList[row].name = newData.name;
    12. connectionList[row].hostAddress = newData.hostAddress;
    13. connectionList[row].username = newData.username;
    14. connectionList[row].password = newData.password;
    15. connectionList[row].port = newData.port;
    16. connectionList[row].sessionType = newData.sessionType;
    17.  
    18. emit dataChanged( index(row, 0), index(row, 0));
    19. return true;
    20. }
    21. return false;
    22. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong now?
    Paul

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Coin3d + Qt: SIGLNALs and SLOTs
    By vonCZ in forum Newbie
    Replies: 26
    Last Post: 15th May 2009, 07:34
  3. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30
  4. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 13:54
  5. TreeView custom model
    By steg90 in forum Newbie
    Replies: 1
    Last Post: 9th May 2007, 10:06

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
  •  
Qt is a trademark of The Qt Company.