Hey all

I have a problem with a QAbstractListModel. I have a model that contains a list of custom classes type server. Of that list I want to show some information from each server item in the list in my gui. So I made a model in which I subclassed QAbstractListModel. When my application ends I write the list to a file and I load that file on the next startup of the application

But here is my problem when I run the program for the first time and there doesn't exist a file with the list I get an error when adding new servers to the list. The first item I add doesn't throw an error but also isn't visible in the gui but when I add a second item I get the following error in my Application Output window in QtCreator:
Qt Code:
  1. QTreeView::rowsInserted internal representation of the model has been corrupted, resetting.
To copy to clipboard, switch view to plain text mode 

But when I close the application and I restart it, it loads the servers I just added so they are written to the file and shows them in the gui. After that I can add servers and they are shown in the gui.

I run Windows 7 64 bit, program in QtCreator 1.2.0 with Qt 4.5.2

Here is my code:
Qt Code:
  1. #include <QDir>
  2. #include <QMessageBox>
  3.  
  4. #include "serverlist.h"
  5.  
  6.  
  7.  
  8. serverList::serverList(): QAbstractListModel()
  9. {
  10. settings = new Settings();
  11. serverDataFile = new QFile(settings->getappDataDir() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
  12. serverDataFileInfo = new QFileInfo(settings->getappDataDir() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
  13. QList<Server> listofServers = QList<Server>();
  14. QDataStream outputStream(serverDataFile);
  15. QDataStream inputStream(serverDataFile);
  16. this->loadServers();
  17. }
  18.  
  19. void serverList::saveServers()
  20. {
  21. if(!serverDataFileInfo->exists())
  22. {
  23. QDir newFile(serverDataFileInfo->absoluteFilePath());
  24. newFile.mkpath(serverDataFileInfo->absolutePath());
  25. }
  26.  
  27. if(!serverDataFile->open(QIODevice::ReadWrite))
  28. {
  29. QMessageBox::critical( 0, "Post program","There was a problem writing the server settings file" );
  30. return;
  31. }
  32. outputStream.setDevice(serverDataFile);
  33. outputStream << this->listofServers;
  34. serverDataFile->close();
  35. return;
  36. }
  37.  
  38. void serverList::addServer(Server newServer)
  39. {
  40. if(!this->listofServers.contains(newServer))
  41. {
  42. this->insertRows(this->listofServers.size(),1,newServer,QModelIndex());
  43. this->saveServers();
  44. }
  45. }
  46.  
  47. void serverList::loadServers()
  48. {
  49. if(!serverDataFileInfo->exists())
  50. {
  51. QDir newFile(serverDataFileInfo->absoluteFilePath());
  52. newFile.mkpath(serverDataFileInfo->absolutePath());
  53. }
  54.  
  55. if(!serverDataFile->open(QIODevice::ReadWrite))
  56. {
  57. QMessageBox::critical( 0, "Post program","There was a problem reading the server settings file" );
  58. return;
  59. }
  60.  
  61. inputStream.setDevice(serverDataFile);
  62. QList<Server> tempList;
  63. inputStream >> tempList;
  64. this->listofServers = tempList;
  65. serverDataFile->close();
  66. return;
  67. }
  68.  
  69. int serverList::columnCount(const QModelIndex &parent) const
  70. {
  71. if( listofServers.count() == 0)
  72. {
  73. return 0;
  74. }
  75. else
  76. {
  77. return listofServers.first().getColumnCount();
  78. }
  79. }
  80.  
  81. int serverList::rowCount(const QModelIndex &parent) const
  82. {
  83. return listofServers.count();
  84. }
  85.  
  86. QVariant serverList::data(const QModelIndex &index, int role) const
  87. {
  88. if (!index.isValid())
  89. return QVariant();
  90.  
  91. if (index.row() >= listofServers.size() || index.row() < 0)
  92. return QVariant();
  93.  
  94. if (role == Qt::DisplayRole) {
  95. if (index.column() == 0)
  96. return listofServers.at(index.row()).getAddres();
  97. else if (index.column() == 1)
  98. return listofServers.at(index.row()).getPort();
  99. else if (index.column() == 2)
  100. return listofServers.at(index.row()).getNickname();
  101. }
  102. return QVariant();
  103. }
  104.  
  105. QVariant serverList::headerData(int section, Qt::Orientation orientation, int role) const
  106. {
  107. if (role != Qt::DisplayRole)
  108. return QVariant();
  109.  
  110. if (orientation == Qt::Horizontal) {
  111. switch (section) {
  112. case 0:
  113. return tr("Server Address");
  114.  
  115. case 1:
  116. return tr("Server Port");
  117.  
  118. case 2:
  119. return tr("Server Nickname");
  120.  
  121. default:
  122. return QVariant();
  123. }
  124. }
  125. return QVariant();
  126. }
  127.  
  128. bool serverList::insertRows(int position, int rows, Server newServer, const QModelIndex &index)
  129. {
  130. Q_UNUSED(index);
  131. beginInsertRows(QModelIndex(), position, position+rows-1);
  132.  
  133. for(int row = 0; row<rows; row++)
  134. {
  135. this->listofServers.insert(position,newServer);
  136. }
  137.  
  138. endInsertRows();
  139. return true;
  140. }
To copy to clipboard, switch view to plain text mode