PDA

View Full Version : custom table model is not working



aurora
7th May 2012, 09:02
Hello,
I developed a custom table model for displaying the contents of a text file….But nothing is displaying in the view…
Please tell me whats changes i need to do for this program….

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore>
#include <QtGui>

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

private:
Ui::MainWindow *ui;
};





class MulModel : public QAbstractTableModel
{
public:
MulModel( QString filename, QObject *parent = 0 );
Qt::ItemFlags flags( const QModelIndex &index ) const;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
int columnCount( const QModelIndex &parent = QModelIndex() ) const;

void ReadFromFile(QString filename);
void PutToModel(QStringList lines);

QString cell_value(int row, int col);
void skipToRow(int row);



private:
QString file;
QStringList LineList;
int m_rows;
int m_columns;
};
#endif // MAINWINDOW_H





mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtGui>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}



MulModel::MulModel( QString filename, QObject *parent ) :
QAbstractTableModel( parent )
{
file=filename;
ReadFromFile(file);
}



void MulModel::ReadFromFile(QString file)
{
QStringList AllLinesOfFile;
QString line;
QFile filename(file);
if( !filename.exists() )
{
cout<<"FILE DOES NOT EXIST---------------------------!"<<endl;
}
if( !filename.open(QIODevice::ReadOnly ) ) // It exists, open it
{
cout<<"FILE COULD NOT OPENED---------!!!!"<<endl;
}
int loop_control=0;
while((loop_control==0) && !filename.atEnd())
{
line=filename.readLine();
if(line.contains("------------"))
{
loop_control=1;
}
}
while(!filename.atEnd()) //while((c==1 && !f.atEnd()))
{
line=filename.readLine();
AllLinesOfFile.append(line);
}
filename.close();
PutToModel(AllLinesOfFile);
}


void MulModel::PutToModel(QStringList lines)
{

int row=0;
int colCount;
foreach(QString line,lines)
{
if(!line.contains("END"))
{


QStandardItemModel *model = new QStandardItemModel;
LineList=line.split(" ",QString::SkipEmptyParts);
colCount=LineList.count();


for ( int col = 0; col < colCount; ++col )
{
QList<QStandardItem *> Lrow;

QStandardItem *item = new QStandardItem;
item->setData(LineList.at(col), Qt::DisplayRole);

Lrow.append(item);


model->appendRow(Lrow);
}

row ++;
}
m_columns=colCount;
m_rows=row;
}

cout<<" row count:"<<m_rows<<" col:"<<m_columns<<endl;

}


int MulModel::rowCount( const QModelIndex &parent ) const
{
cout <<"Called RowCount: "<<m_rows<<endl;
return m_rows;
}




int MulModel::columnCount( const QModelIndex &parent ) const
{
cout <<"Called Column Count: "<<m_columns<<endl;
return m_columns;
}



QVariant MulModel::data( const QModelIndex &index, int role ) const
{
cout<<" data------------row:"<<index.row()<<endl;
if (!index.isValid())
return QVariant::Invalid;

if (index.row() >= LineList.size())
return QVariant::Invalid;

if (role == Qt::DisplayRole)
return LineList.at(index.row());
else
return QVariant::Invalid;

}


QVariant MulModel::headerData( int section,
Qt::Orientation orientation, int role ) const
{
cout << "Called HeaderData, role = " << role <<endl;
if( role != Qt::DisplayRole )
return QVariant::Invalid;

cout<<" HEDDD:"<<section+1<<endl;
return QString::number(section+1);
}



Qt::ItemFlags MulModel::flags( const QModelIndex &index ) const
{
if(!index.isValid())
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
return Qt::NoItemFlags;
}

void MainWindow::on_pushButton_clicked()
{

QString fileName = QFileDialog::getOpenFileName(this,
tr("Open tracker file"), "C:/Users/dnaik1/Desktop/fd", tr("tracker Files (*.txt)"));
ui->lineEdit->setText(fileName);


}

void MainWindow::on_pushButton_2_clicked()
{
//MulModel model( 12, 12 );
//QTableView table;
//table.setModel( &model );
//table.show();
MulModel model(ui->lineEdit->text());
ui->tableView->setModel( &model);


}




main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtCore>
#include <QtGui>
int main( int argc, char **argv )
{
QApplication app( argc, argv );


MainWindow w;
w.show();

return app.exec();
}

aurora
8th May 2012, 14:08
please somebody reply.....
my final goal is to display very large text file (qround 1gb)...
what should i do?
will this model view helps me?

d_stranz
8th May 2012, 17:15
For one thing, you shouldn't be combining two QObject-derived classes in one pair of cpp and h files. Split them into one pair of cpp and h for MainWindow, and one pair for MulModel.

For another, you haven't declared Q_OBJECT in your MulModel class so none of the signals and slots for the base class will work.

For a third, in line 73 of mainwindow.cpp, you create a new QStandardItemModel, even though your MulModel class already *is* a model. You then fill this new model pointer up with stuff, and then let the pointer go out of scope when the function exits. How do you get at the contents of this dangling pointer to a model inside a model after that?

And for a fourth, in line 172 of mainwindow.cpp, you create an instance of your MulModel class on the stack, proceed to fill it up with data, and then let it go out of scope (and get destroyed) when the method exits.

So what does the table have to work with when you give it a pointer to an object that's about to be deleted, and that object has a dangling pointer that actually contains all the data but is completely inaccessible?

norobro
8th May 2012, 20:02
You're making this overly complicated.

Simply load the data from your file into a container, say QList<QStringList> LineList. Then in the model's data function return LineList[index.row()][index.column()].

As d_stranz says, you need to create MulModel on the heap.