Results 1 to 4 of 4

Thread: Problem with QAbstractListModel

  1. #1
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QAbstractListModel

    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 

  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: Problem with QAbstractListModel

    Hmm... doesn't your implementation add items to the list in reverse order?

    Unfortunately apart from that your implementation seems correct... Is this the exact code you have?
    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
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QAbstractListModel

    This is the exact code how I use it.

    I will post some extra code maybe I made a mistake there:

    This is the code where I create the list and add the model:
    Qt Code:
    1. void serverView::createInterface()
    2. {
    3. QHBoxLayout *layout = new QHBoxLayout;
    4.  
    5. listServers = new QTreeView;
    6. serversList = new serverList();
    7.  
    8. listServers->setAlternatingRowColors(true);
    9. listServers->setSelectionBehavior(QAbstractItemView::SelectRows);
    10. listServers->setSortingEnabled(true);
    11. listServers->setSelectionMode(QAbstractItemView::SingleSelection);
    12.  
    13. proxyModel->setSourceModel(serversList);
    14. listServers->setModel(proxyModel);
    15.  
    16. layout->addWidget(listServers);
    17. setLayout(layout);
    18. }
    To copy to clipboard, switch view to plain text mode 


    This is my Server class:
    server.cpp
    Qt Code:
    1. #include "server.h"
    2. #include <QDataStream>
    3. #include <QString>
    4. #include <QDebug>
    5.  
    6. Server::Server()
    7. {
    8. }
    9.  
    10. QDataStream& operator<<(QDataStream& out, const Server& server)
    11. {
    12. out << server.getAddres();
    13. out << server.getNickname();
    14. out << server.getPort();
    15. out << server.getConnections();
    16. out << server.getUsername();
    17. out << server.getPassword();
    18. out << server.getRequireLogin();
    19. out << server.getUseSsl();
    20. return out;
    21. }
    22. QDataStream& operator>>(QDataStream& in,Server& server)
    23. {
    24. QString tempA, tempN, tempUn, tempPa;
    25. qint32 tempPo, tempC;
    26. bool tempR, tempUS;
    27.  
    28. in >> tempA;
    29. server.setAddres(tempA);
    30. in >> tempN;
    31. server.setNickname(tempN);
    32. in >> tempPo;
    33. server.setPort(tempPo);
    34. in >> tempC;
    35. server.setConnections(tempC);
    36. in >> tempUn;
    37. server.setUsername(tempUn);
    38. in >> tempPa;
    39. server.setPassword(tempPa);
    40. in >> tempR;
    41. server.setRequireLogin(tempR);
    42. in >> tempUS;
    43. server.setUseSsl(tempUS);
    44. return in;
    45. }
    To copy to clipboard, switch view to plain text mode 


    server.h
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QString>
    5. #include <QtGlobal>
    6.  
    7.  
    8. class Server
    9. {
    10. public:
    11. Server();
    12.  
    13.  
    14. //Set functions
    15. void setAddres(QString newAddres) { serverAddres = newAddres; }
    16. void setNickname(QString newNickname) { serverNickname = newNickname; }
    17. void setPort(qint32 newPort) {serverPort = newPort; }
    18. void setConnections(qint32 newConnections) { serverConnections = newConnections; }
    19. void setUsername(QString newUsername) { serverUsername = newUsername; }
    20. void setPassword(QString newPassword) { serverPassword = newPassword;}
    21. void setRequireLogin(bool newRequireLogin) { serverRequireLogin = newRequireLogin; }
    22. void setUseSsl(bool newUseSsl) { serverUseSsl = newUseSsl; }
    23.  
    24. //Get functions
    25. QString getAddres() { return serverAddres; }
    26. QString getAddres() const { return serverAddres; }
    27. QString getNickname() { return serverNickname; }
    28. QString getNickname() const { return serverNickname; }
    29. qint32 getPort() { return serverPort; }
    30. qint32 getPort() const { return serverPort; }
    31. qint32 getConnections() { return serverConnections; }
    32. qint32 getConnections() const { return serverConnections; }
    33. QString getUsername() { return serverUsername; }
    34. QString getUsername() const { return serverUsername; }
    35. QString getPassword() { return serverPassword; }
    36. QString getPassword() const { return serverPassword; }
    37. bool getRequireLogin() { return serverRequireLogin; }
    38. bool getRequireLogin() const { return serverRequireLogin; }
    39. bool getUseSsl() { return serverUseSsl; }
    40. bool getUseSsl() const { return serverUseSsl; }
    41.  
    42. int getColumnCount() { return 3; }
    43. int getColumnCount() const { return 3; }
    44.  
    45.  
    46.  
    47. private:
    48. QString serverAddres;
    49. QString serverNickname;
    50. qint32 serverPort;
    51. qint32 serverConnections;
    52. QString serverUsername;
    53. QString serverPassword;
    54. bool serverRequireLogin;
    55. bool serverUseSsl;
    56.  
    57. };
    58.  
    59. QDataStream& operator<<(QDataStream& out, const Server& server);
    60. QDataStream& operator>>(QDataStream& in,Server& server);
    61.  
    62. inline bool operator==(Server s1, Server s2) { return (s1.getAddres() == s2.getAddres() && s1.getUsername() == s2.getUsername() && s1.getPassword() == s2.getPassword() && s1.getUseSsl() == s2.getUseSsl() );}
    63. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QAbstractListModel

    I think I have found the solution. In the function columnCount from my serverList class I return 0 when the list is empty so I think it remembers that and when I add servers it gives an error on that because I now have set that to return 3 and now it works.

Similar Threads

  1. Replies: 19
    Last Post: 4th April 2009, 00:17
  2. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 13:05
  3. deployment problem: msvc++ 2008 Express, Qt 4.4.3
    By vonCZ in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2008, 15:38
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 13:45
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 22:36

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.