Valheru
28th August 2006, 08:49
I'm developing an binary news reader. In the end I'm using an sqlite database to store the data in, but I'm first trying to get to grips with the model/view structure by using a simple QStringList to hold the data.
I'm subclassing the QAbstractItemModel to achieve all this, but I'm a bit lost. I did this once before in Java for a school assignment, but I can't remember much of it. At any rate, Qt's way of doing it has got me stumped.
I don't really understand what I'm doing, despite having read the relavent documentation and looking at the examples. What I have achieved works, except anything added to the string list doesn't show up in the tree view. It is added to the array, as you can see in the code I checked that using a QMessageBox.
/************************************************** *************************
* Copyright (C) 2006 by Lawrence Lee *
* valheru@facticius.net *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
************************************************** *************************/
#include <QHeaderView>
#include <QMessageBox>
#include "serverListModel.h"
serverListModel::serverListModel( QObject *parent )
{
p = parent;
}
serverListModel::~serverListModel()
{
}
QModelIndex serverListModel::index( int row, int column, const QModelIndex &parent ) const
{
return QModelIndex();
}
QModelIndex serverListModel::parent( const QModelIndex &index ) const
{
return QModelIndex();
}
int serverListModel::rowCount( const QModelIndex &parent ) const
{
return list.count();
}
int serverListModel::columnCount( const QModelIndex &parent ) const
{
if( p->objectName() == "serverTreeList" ){
return 1;
}
if( p->objectName() == "articleTreeList" ){
return 3;
}
}
QVariant serverListModel::data( const QModelIndex &index, int role ) const
{
return list.at( index.row() );
}
QVariant serverListModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if( role != Qt::DisplayRole ){
return QVariant();
}else{
if( p->objectName() == "serverTreeList" ){
return QString( "Server List" );
}
if( p->objectName() == "articleTreeList" ){
return QString( "Article List" );
}
}
}
Qt::ItemFlags serverListModel::flags( const QModelIndex &index ) const
{
return Qt::ItemIsEnabled;
}
bool serverListModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
list.replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
bool serverListModel::insertRows( int position, int rows, const QModelIndex &parent )
{
beginInsertRows( QModelIndex(), position, position + rows - 1 );
for( int row = 0; row < rows; ++row ){
list.insert(position, "");
}
endInsertRows();
return true;
}
bool serverListModel::removeRows( int position, int rows, const QModelIndex &parent )
{
beginRemoveRows( QModelIndex(), position, position + rows - 1 );
for( int row = 0; row < rows; ++row ){
list.removeAt(position);
}
endRemoveRows();
return true;
}
void serverListModel::addItem( const QString& item )
{
QModelIndex index;
beginInsertRows( index, 1, 1 );
list.insert(list.size(), item);
endInsertRows();
for( int i = 0; i < list.size(); i++ ){
QMessageBox *msg = new QMessageBox( "list", "list[" + QString::number(i) + "] = " + list[i],
QMessageBox::Information, QMessageBox::Ok, 0, 0 );
msg->exec();
}
}
If someone could take the time to explain how the whole model/view thing works wtih regards to the QModelIndex that passed to all the functions you have to reimplement I'd be much obliged, but I'd settle for knowing why my tree view doesn't reflect the changes made to the string list :)
I'm subclassing the QAbstractItemModel to achieve all this, but I'm a bit lost. I did this once before in Java for a school assignment, but I can't remember much of it. At any rate, Qt's way of doing it has got me stumped.
I don't really understand what I'm doing, despite having read the relavent documentation and looking at the examples. What I have achieved works, except anything added to the string list doesn't show up in the tree view. It is added to the array, as you can see in the code I checked that using a QMessageBox.
/************************************************** *************************
* Copyright (C) 2006 by Lawrence Lee *
* valheru@facticius.net *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
************************************************** *************************/
#include <QHeaderView>
#include <QMessageBox>
#include "serverListModel.h"
serverListModel::serverListModel( QObject *parent )
{
p = parent;
}
serverListModel::~serverListModel()
{
}
QModelIndex serverListModel::index( int row, int column, const QModelIndex &parent ) const
{
return QModelIndex();
}
QModelIndex serverListModel::parent( const QModelIndex &index ) const
{
return QModelIndex();
}
int serverListModel::rowCount( const QModelIndex &parent ) const
{
return list.count();
}
int serverListModel::columnCount( const QModelIndex &parent ) const
{
if( p->objectName() == "serverTreeList" ){
return 1;
}
if( p->objectName() == "articleTreeList" ){
return 3;
}
}
QVariant serverListModel::data( const QModelIndex &index, int role ) const
{
return list.at( index.row() );
}
QVariant serverListModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if( role != Qt::DisplayRole ){
return QVariant();
}else{
if( p->objectName() == "serverTreeList" ){
return QString( "Server List" );
}
if( p->objectName() == "articleTreeList" ){
return QString( "Article List" );
}
}
}
Qt::ItemFlags serverListModel::flags( const QModelIndex &index ) const
{
return Qt::ItemIsEnabled;
}
bool serverListModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
list.replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
bool serverListModel::insertRows( int position, int rows, const QModelIndex &parent )
{
beginInsertRows( QModelIndex(), position, position + rows - 1 );
for( int row = 0; row < rows; ++row ){
list.insert(position, "");
}
endInsertRows();
return true;
}
bool serverListModel::removeRows( int position, int rows, const QModelIndex &parent )
{
beginRemoveRows( QModelIndex(), position, position + rows - 1 );
for( int row = 0; row < rows; ++row ){
list.removeAt(position);
}
endRemoveRows();
return true;
}
void serverListModel::addItem( const QString& item )
{
QModelIndex index;
beginInsertRows( index, 1, 1 );
list.insert(list.size(), item);
endInsertRows();
for( int i = 0; i < list.size(); i++ ){
QMessageBox *msg = new QMessageBox( "list", "list[" + QString::number(i) + "] = " + list[i],
QMessageBox::Information, QMessageBox::Ok, 0, 0 );
msg->exec();
}
}
If someone could take the time to explain how the whole model/view thing works wtih regards to the QModelIndex that passed to all the functions you have to reimplement I'd be much obliged, but I'd settle for knowing why my tree view doesn't reflect the changes made to the string list :)