eekhoorn12
25th August 2009, 22:03
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:
QTreeView::rowsInserted internal representation of the model has been corrupted, resetting.
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:
#include <QDir>
#include <QMessageBox>
#include "serverlist.h"
serverList::serverList(): QAbstractListModel()
{
settings = new Settings();
serverDataFile = new QFile(settings->getappDataDir() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
serverDataFileInfo = new QFileInfo(settings->getappDataDir() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
QList<Server> listofServers = QList<Server>();
QDataStream outputStream(serverDataFile);
QDataStream inputStream(serverDataFile);
this->loadServers();
}
void serverList::saveServers()
{
if(!serverDataFileInfo->exists())
{
QDir newFile(serverDataFileInfo->absoluteFilePath());
newFile.mkpath(serverDataFileInfo->absolutePath());
}
if(!serverDataFile->open(QIODevice::ReadWrite))
{
QMessageBox::critical( 0, "Post program","There was a problem writing the server settings file" );
return;
}
outputStream.setDevice(serverDataFile);
outputStream << this->listofServers;
serverDataFile->close();
return;
}
void serverList::addServer(Server newServer)
{
if(!this->listofServers.contains(newServer))
{
this->insertRows(this->listofServers.size(),1,newServer,QModelIndex());
this->saveServers();
}
}
void serverList::loadServers()
{
if(!serverDataFileInfo->exists())
{
QDir newFile(serverDataFileInfo->absoluteFilePath());
newFile.mkpath(serverDataFileInfo->absolutePath());
}
if(!serverDataFile->open(QIODevice::ReadWrite))
{
QMessageBox::critical( 0, "Post program","There was a problem reading the server settings file" );
return;
}
inputStream.setDevice(serverDataFile);
QList<Server> tempList;
inputStream >> tempList;
this->listofServers = tempList;
serverDataFile->close();
return;
}
int serverList::columnCount(const QModelIndex &parent) const
{
if( listofServers.count() == 0)
{
return 0;
}
else
{
return listofServers.first().getColumnCount();
}
}
int serverList::rowCount(const QModelIndex &parent) const
{
return listofServers.count();
}
QVariant serverList::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= listofServers.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole) {
if (index.column() == 0)
return listofServers.at(index.row()).getAddres();
else if (index.column() == 1)
return listofServers.at(index.row()).getPort();
else if (index.column() == 2)
return listofServers.at(index.row()).getNickname();
}
return QVariant();
}
QVariant serverList::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("Server Address");
case 1:
return tr("Server Port");
case 2:
return tr("Server Nickname");
default:
return QVariant();
}
}
return QVariant();
}
bool serverList::insertRows(int position, int rows, Server newServer, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position+rows-1);
for(int row = 0; row<rows; row++)
{
this->listofServers.insert(position,newServer);
}
endInsertRows();
return true;
}
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:
QTreeView::rowsInserted internal representation of the model has been corrupted, resetting.
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:
#include <QDir>
#include <QMessageBox>
#include "serverlist.h"
serverList::serverList(): QAbstractListModel()
{
settings = new Settings();
serverDataFile = new QFile(settings->getappDataDir() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
serverDataFileInfo = new QFileInfo(settings->getappDataDir() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
QList<Server> listofServers = QList<Server>();
QDataStream outputStream(serverDataFile);
QDataStream inputStream(serverDataFile);
this->loadServers();
}
void serverList::saveServers()
{
if(!serverDataFileInfo->exists())
{
QDir newFile(serverDataFileInfo->absoluteFilePath());
newFile.mkpath(serverDataFileInfo->absolutePath());
}
if(!serverDataFile->open(QIODevice::ReadWrite))
{
QMessageBox::critical( 0, "Post program","There was a problem writing the server settings file" );
return;
}
outputStream.setDevice(serverDataFile);
outputStream << this->listofServers;
serverDataFile->close();
return;
}
void serverList::addServer(Server newServer)
{
if(!this->listofServers.contains(newServer))
{
this->insertRows(this->listofServers.size(),1,newServer,QModelIndex());
this->saveServers();
}
}
void serverList::loadServers()
{
if(!serverDataFileInfo->exists())
{
QDir newFile(serverDataFileInfo->absoluteFilePath());
newFile.mkpath(serverDataFileInfo->absolutePath());
}
if(!serverDataFile->open(QIODevice::ReadWrite))
{
QMessageBox::critical( 0, "Post program","There was a problem reading the server settings file" );
return;
}
inputStream.setDevice(serverDataFile);
QList<Server> tempList;
inputStream >> tempList;
this->listofServers = tempList;
serverDataFile->close();
return;
}
int serverList::columnCount(const QModelIndex &parent) const
{
if( listofServers.count() == 0)
{
return 0;
}
else
{
return listofServers.first().getColumnCount();
}
}
int serverList::rowCount(const QModelIndex &parent) const
{
return listofServers.count();
}
QVariant serverList::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= listofServers.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole) {
if (index.column() == 0)
return listofServers.at(index.row()).getAddres();
else if (index.column() == 1)
return listofServers.at(index.row()).getPort();
else if (index.column() == 2)
return listofServers.at(index.row()).getNickname();
}
return QVariant();
}
QVariant serverList::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("Server Address");
case 1:
return tr("Server Port");
case 2:
return tr("Server Nickname");
default:
return QVariant();
}
}
return QVariant();
}
bool serverList::insertRows(int position, int rows, Server newServer, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position+rows-1);
for(int row = 0; row<rows; row++)
{
this->listofServers.insert(position,newServer);
}
endInsertRows();
return true;
}