Problem in subclassing QTable
-
wrote on 2 Mar 2012, 03:56 last edited by
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.... -
wrote on 2 Mar 2012, 07:19 last edited by
There is no such class as QTable. Read docs. You have there QTableWidget and QTableView classes.If you subclass first than you get ready to go table, if you subclass second you need to set model for it. In main function you call show() method for this class but there is no realization of this method. Also I recommended you to put description of class in header files and realizations in cpp, and make one header and one cpp for each class, it makes your program more readable.
-
wrote on 2 Mar 2012, 07:58 last edited by
What Qt Quarterly edition does that sample come from? "QTable":http://doc.trolltech.com/3.3/qtable.html is a class from Qt 3, which is very old. I would not recommend using anything from that era for new code, nor to use any of the qt3support classes in new code.
-
wrote on 2 Mar 2012, 08:29 last edited by
[quote author="Andre" date="1330675102"]What Qt Quarterly edition does that sample come from? "QTable":http://doc.trolltech.com/3.3/qtable.html is a class from Qt 3, which is very old. I would not recommend using anything from that era for new code, nor to use any of the qt3support classes in new code. [/quote]
Thank u Andre for ur suggestion...
I want to display a large TSV(Tab separated Values) files in table format...when i tried using model/view, i could able to load a file of 100MB, but i hav files of size 1.5GB..!!!
What should i do?
Please suggest me some idea -
wrote on 2 Mar 2012, 08:39 last edited by
In general, displaying data that big is going to be an issue in any case. Perhaps your in-memory representation of the data can be smaller than the original file size? That can be the case if the original file contains lots of numbers and dates and the likes, as these can be more efficiently represented in memory than in a text file. In that case, you may still be able to load the whole monster in memory and create a simple QAbstractTableModel on on top of that data storage. If not, then you will need to figure out a way to work with the file in chunks. You might be able to memory-map the file and build up some kind of index over it to navigate it quickly (perhaps even in a separate thread), or you'd have to think of another solution, like storing your data in a database instead and use the database-based model-view classes.
You should also ask yourself: do I really need to display all data in one ginormous table in one go? How are your users supposed to use that table? Do you really want them to scroll through the whole thing?
-
wrote on 2 Mar 2012, 09:41 last edited by
[quote author="Andre" date="1330677575"]In that case, you may still be able to load the whole monster in memory and create a simple QAbstractTableModel on on top of that data storage.[/quote]
Thank u..but which kind of data structure is good to store the whole file contents? and the 95% of the contents are numbers of diffrent length(8 to 266 digit in some case..)
-
wrote on 2 Mar 2012, 10:06 last edited by
How should we know, if you don't tell us details on the data structure and the kinds of operations you need to perform on it?
There are no standard types that can hold numbers of 266 digits, at least not with that full precision.
-
wrote on 2 Mar 2012, 10:48 last edited by
ok...
1.My files are all tab spaced values..each file size may range from few MB's to 1.5Gb..
2. Most of the values in the table are number (1 to 256 digit), but text also present
3. I want to show this file content in a table format...
4. In that table i may perform searching operation for particular value...I tried using model view...code is shown below....
But using this i was unable to load files more than 50Mb...:(Please suggest me what else i can do?
@ model = new QStandardItemModel(this);
while(!f.atEnd())
{
line=f.readLine();LineList=line.split(" ",QString::SkipEmptyParts); int row =model->rowCount(); model->insertRow(row); for(int i=0;i<LineList.count();i++) { QStandardItem *fileNameItem=new QStandardItem(LineList.at(i)); fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable); model->setItem(row,i,fileNameItem); } } f.close(); table=new QTableView(); table->setModel(model); ui->tabWidget->addTab(table,"NEW TAB");@
1/8