QTreeView::rowsInserted internal representation of the model has been corrupted, resetting.
QTreeView::rowsInserted internal representation of the model has been corrupted, resetting.
To copy to clipboard, switch view to plain text mode
#include <QDir>
#include <QMessageBox>
#include "serverlist.h"
{
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>();
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();
}
{
if (!index.isValid())
if (index.row() >= listofServers.size() || index.row() < 0)
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();
}
}
QVariant serverList
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if (role != Qt::DisplayRole)
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:
}
}
}
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks