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....
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....