PDA

View Full Version : problem in subclassing QTable



aurora
2nd March 2012, 03:53
I am trying to subclass QTable as shown below(Actually i referred this code from Qt quarterly)
But i'm getting compiler error saying "mainwindow.h:59: error: expected class-name before '{' token"


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCore>
#include <QtGui>



namespace Ui {
class TsvDataSource;
}



class DataSource : public QObject
{
Q_OBJECT
public:
DataSource();


virtual QString cell(int row, int col) const = 0;
// virtual void setCell(int row, int col, const QString &text) = 0;

virtual int numRows() const = 0;
virtual int numCols() const = 0;


};





class TsvDataSource : public DataSource
{
Q_OBJECT
public:
TsvDataSource(const QString &fileName);

QString cell(int row, int col) const;

int numRows() const { return nRows; }
int numCols() const { return nCols; }

private:
void skipToRow(int row) const;

private:
mutable QFile file;
int nRows;
int nCols;
};



class Table : public QTable
{
Q_OBJECT
public:
Table(DataSource *dSource, QWidget *parent = 0, const char *name = 0);

QString text(int row, int col) const;


private:
DataSource *dataSource;

};


#endif // MAINWINDOW_H



mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtGui>

Table::Table(DataSource *dSource, QWidget *parent, const char *name)
: QTable(dSource->numRows(), dSource->numCols(), parent, name),
dataSource(dSource), editor(0)
{
connect(dataSource, SIGNAL(dataChanged()), this, SLOT(updateContents()));
}


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


TsvDataSource::TsvDataSource(const QString &fileName)
: file(fileName)
{
nRows = nCols = 0;
if (!file.open(IO_ReadWrite)) {
qWarning(QString("Couldn't open %1: %2").arg(fileName).arg(file.errorString()));
} else {
QString line;
while (file.readLine(line, 32767) != -1) {
if (nRows == 0)
nCols = line.contains('\t') + 1;
++nRows;
}
}
}


QString TsvDataSource::cell(int row, int col) const
{
skipToRow(row);
QString line;
file.readLine(line, 32767);
return line.section('\t', col, col);
}

void TsvDataSource::skipToRow(int row) const
{
file.reset();
QString line;
int i = 0;
while (i++ < row)
file.readLine(line, 32767);
}



QString Table::text(int row, int col) const
{
return dataSource->cell(row, col);
}



main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TsvDataSource dataSource("test.tsv");
Table table1(&dataSource);

table1.show();


return app.exec();
return app.exec();
}



Please somebody help me to solve this...
I'm also getting doubt whether there really exists just "QTable" other than "QTableWidget" and "QTableView"...:P
Please dont mind i'm new to Qt, my question may be very silly....

ChrisW67
2nd March 2012, 04:44
There's no error at or before line 16. There is an obvious error at line 58 that causes an error message at line 59: what is QTable and where is it defined? There's no doubt.

In future it helps if you post the actual code that generated the error so that the line numbers are meaningful, or adjust the error message to match the line numbers the forum gives a snippet.

aurora
2nd March 2012, 04:58
Thank u Chris for reply...ya as u pointed out, the error was at 59, where the declaration

class Table : public QTable
{
Q_OBJECT.
.
.


what should i do for this?
Please hav a look at this...http://doc.qt.nokia.com/qq/qq07-big-tables.html
i just copied code from there...my requirement as they specified i wanted to load big files in table

ChrisW67
2nd March 2012, 05:42
QTable (http://doc.trolltech.com/3.3/qtable.html) is part of Qt 3, not Qt4. Its closest Qt4 equivalent is QTableView, but the API is different. The equivalent Qt4 approach to reading your TSV files would be to subclass QAbstractTableModel, the details of which are covered in the Model/View Programming docs.