Results 1 to 4 of 4

Thread: problem in subclassing QTable

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtCore>
    6. #include <QtGui>
    7.  
    8.  
    9.  
    10. namespace Ui {
    11. class TsvDataSource;
    12. }
    13.  
    14.  
    15.  
    16. class DataSource : public QObject
    17. {
    18. Q_OBJECT
    19. public:
    20. DataSource();
    21.  
    22.  
    23. virtual QString cell(int row, int col) const = 0;
    24. // virtual void setCell(int row, int col, const QString &text) = 0;
    25.  
    26. virtual int numRows() const = 0;
    27. virtual int numCols() const = 0;
    28.  
    29.  
    30. };
    31.  
    32.  
    33.  
    34.  
    35.  
    36. class TsvDataSource : public DataSource
    37. {
    38. Q_OBJECT
    39. public:
    40. TsvDataSource(const QString &fileName);
    41.  
    42. QString cell(int row, int col) const;
    43.  
    44. int numRows() const { return nRows; }
    45. int numCols() const { return nCols; }
    46.  
    47. private:
    48. void skipToRow(int row) const;
    49.  
    50. private:
    51. mutable QFile file;
    52. int nRows;
    53. int nCols;
    54. };
    55.  
    56.  
    57.  
    58. class Table : public QTable
    59. {
    60. Q_OBJECT
    61. public:
    62. Table(DataSource *dSource, QWidget *parent = 0, const char *name = 0);
    63.  
    64. QString text(int row, int col) const;
    65.  
    66.  
    67. private:
    68. DataSource *dataSource;
    69.  
    70. };
    71.  
    72.  
    73. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. Table::Table(DataSource *dSource, QWidget *parent, const char *name)
    7. : QTable(dSource->numRows(), dSource->numCols(), parent, name),
    8. dataSource(dSource), editor(0)
    9. {
    10. connect(dataSource, SIGNAL(dataChanged()), this, SLOT(updateContents()));
    11. }
    12.  
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19.  
    20. TsvDataSource::TsvDataSource(const QString &fileName)
    21. : file(fileName)
    22. {
    23. nRows = nCols = 0;
    24. if (!file.open(IO_ReadWrite)) {
    25. qWarning(QString("Couldn't open %1: %2").arg(fileName).arg(file.errorString()));
    26. } else {
    27. QString line;
    28. while (file.readLine(line, 32767) != -1) {
    29. if (nRows == 0)
    30. nCols = line.contains('\t') + 1;
    31. ++nRows;
    32. }
    33. }
    34. }
    35.  
    36.  
    37. QString TsvDataSource::cell(int row, int col) const
    38. {
    39. skipToRow(row);
    40. QString line;
    41. file.readLine(line, 32767);
    42. return line.section('\t', col, col);
    43. }
    44.  
    45. void TsvDataSource::skipToRow(int row) const
    46. {
    47. file.reset();
    48. QString line;
    49. int i = 0;
    50. while (i++ < row)
    51. file.readLine(line, 32767);
    52. }
    53.  
    54.  
    55.  
    56. QString Table::text(int row, int col) const
    57. {
    58. return dataSource->cell(row, col);
    59. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. TsvDataSource dataSource("test.tsv");
    8. Table table1(&dataSource);
    9.  
    10. table1.show();
    11.  
    12.  
    13. return app.exec();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 


    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....
    Last edited by aurora; 2nd March 2012 at 04:59.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem in subclassing QTable

    Thank u Chris for reply...ya as u pointed out, the error was at 59, where the declaration
    Qt Code:
    1. class Table : public QTable
    2. {
    3. Q_OBJECT.
    4. .
    5. .
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by aurora; 2nd March 2012 at 05:05.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

Similar Threads

  1. Problem Subclassing Calendar
    By Archa4 in forum Newbie
    Replies: 7
    Last Post: 9th March 2011, 11:50
  2. Subclassing QSortFilterProxyModel problem
    By e79ene in forum Newbie
    Replies: 2
    Last Post: 21st February 2011, 13:23
  3. Subclassing QMainWindow problem
    By lerwys in forum Qt Programming
    Replies: 7
    Last Post: 28th April 2009, 08:40
  4. Problem in SubClassing QTableItem.
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2006, 10:21
  5. Problem with QTable
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 10:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.