problem in subclassing QTable
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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
namespace Ui {
class TsvDataSource;
}
{
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:
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
Code:
#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 {
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);
file.readLine(line, 32767);
return line.section('\t', col, col);
}
void TsvDataSource::skipToRow(int row) const
{
file.reset();
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
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *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....
Re: problem in subclassing QTable
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.
Re: problem in subclassing QTable
Thank u Chris for reply...ya as u pointed out, the error was at 59, where the declaration
Code:
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
Re: problem in subclassing QTable
QTable 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.