Results 1 to 4 of 4

Thread: custom table model is not working

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

    Default custom table model is not working

    Hello,
    I developed a custom table model for displaying the contents of a text file….But nothing is displaying in the view…
    Please tell me whats changes i need to do for this program….

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. #include <QMainWindow>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private slots:
    21. void on_pushButton_clicked();
    22.  
    23. void on_pushButton_2_clicked();
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27. };
    28.  
    29.  
    30.  
    31.  
    32.  
    33. class MulModel : public QAbstractTableModel
    34. {
    35. public:
    36. MulModel( QString filename, QObject *parent = 0 );
    37. Qt::ItemFlags flags( const QModelIndex &index ) const;
    38. QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
    39. QVariant headerData( int section, Qt::Orientation orientation,
    40. int role = Qt::DisplayRole ) const;
    41. int rowCount( const QModelIndex &parent = QModelIndex() ) const;
    42. int columnCount( const QModelIndex &parent = QModelIndex() ) const;
    43.  
    44. void ReadFromFile(QString filename);
    45. void PutToModel(QStringList lines);
    46.  
    47. QString cell_value(int row, int col);
    48. void skipToRow(int row);
    49.  
    50.  
    51.  
    52. private:
    53. QString file;
    54. QStringList LineList;
    55. int m_rows;
    56. int m_columns;
    57. };
    58. #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. #include <iostream>
    6. using namespace std;
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19.  
    20.  
    21. MulModel::MulModel( QString filename, QObject *parent ) :
    22. {
    23. file=filename;
    24. ReadFromFile(file);
    25. }
    26.  
    27.  
    28.  
    29. void MulModel::ReadFromFile(QString file)
    30. {
    31. QStringList AllLinesOfFile;
    32. QString line;
    33. QFile filename(file);
    34. if( !filename.exists() )
    35. {
    36. cout<<"FILE DOES NOT EXIST---------------------------!"<<endl;
    37. }
    38. if( !filename.open(QIODevice::ReadOnly ) ) // It exists, open it
    39. {
    40. cout<<"FILE COULD NOT OPENED---------!!!!"<<endl;
    41. }
    42. int loop_control=0;
    43. while((loop_control==0) && !filename.atEnd())
    44. {
    45. line=filename.readLine();
    46. if(line.contains("------------"))
    47. {
    48. loop_control=1;
    49. }
    50. }
    51. while(!filename.atEnd()) //while((c==1 && !f.atEnd()))
    52. {
    53. line=filename.readLine();
    54. AllLinesOfFile.append(line);
    55. }
    56. filename.close();
    57. PutToModel(AllLinesOfFile);
    58. }
    59.  
    60.  
    61. void MulModel::PutToModel(QStringList lines)
    62. {
    63.  
    64. int row=0;
    65. int colCount;
    66. foreach(QString line,lines)
    67. {
    68. if(!line.contains("END"))
    69. {
    70.  
    71.  
    72. LineList=line.split(" ",QString::SkipEmptyParts);
    73. colCount=LineList.count();
    74.  
    75.  
    76. for ( int col = 0; col < colCount; ++col )
    77. {
    78. QList<QStandardItem *> Lrow;
    79.  
    80. item->setData(LineList.at(col), Qt::DisplayRole);
    81.  
    82. Lrow.append(item);
    83.  
    84.  
    85. model->appendRow(Lrow);
    86. }
    87.  
    88. row ++;
    89. }
    90. m_columns=colCount;
    91. m_rows=row;
    92. }
    93.  
    94. cout<<" row count:"<<m_rows<<" col:"<<m_columns<<endl;
    95.  
    96. }
    97.  
    98.  
    99. int MulModel::rowCount( const QModelIndex &parent ) const
    100. {
    101. cout <<"Called RowCount: "<<m_rows<<endl;
    102. return m_rows;
    103. }
    104.  
    105.  
    106.  
    107.  
    108. int MulModel::columnCount( const QModelIndex &parent ) const
    109. {
    110. cout <<"Called Column Count: "<<m_columns<<endl;
    111. return m_columns;
    112. }
    113.  
    114.  
    115.  
    116. QVariant MulModel::data( const QModelIndex &index, int role ) const
    117. {
    118. cout<<" data------------row:"<<index.row()<<endl;
    119. if (!index.isValid())
    120. return QVariant::Invalid;
    121.  
    122. if (index.row() >= LineList.size())
    123. return QVariant::Invalid;
    124.  
    125. if (role == Qt::DisplayRole)
    126. return LineList.at(index.row());
    127. else
    128. return QVariant::Invalid;
    129.  
    130. }
    131.  
    132.  
    133. QVariant MulModel::headerData( int section,
    134. Qt::Orientation orientation, int role ) const
    135. {
    136. cout << "Called HeaderData, role = " << role <<endl;
    137. if( role != Qt::DisplayRole )
    138. return QVariant::Invalid;
    139.  
    140. cout<<" HEDDD:"<<section+1<<endl;
    141. return QString::number(section+1);
    142. }
    143.  
    144.  
    145.  
    146. Qt::ItemFlags MulModel::flags( const QModelIndex &index ) const
    147. {
    148. if(!index.isValid())
    149. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    150. return Qt::NoItemFlags;
    151. }
    152.  
    153. void MainWindow::on_pushButton_clicked()
    154. {
    155.  
    156. QString fileName = QFileDialog::getOpenFileName(this,
    157. tr("Open tracker file"), "C:/Users/dnaik1/Desktop/fd", tr("tracker Files (*.txt)"));
    158. ui->lineEdit->setText(fileName);
    159.  
    160.  
    161. }
    162.  
    163. void MainWindow::on_pushButton_2_clicked()
    164. {
    165. //MulModel model( 12, 12 );
    166. //QTableView table;
    167. //table.setModel( &model );
    168. //table.show();
    169. MulModel model(ui->lineEdit->text());
    170. ui->tableView->setModel( &model);
    171.  
    172.  
    173. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <QtCore>
    4. #include <QtGui>
    5. int main( int argc, char **argv )
    6. {
    7. QApplication app( argc, argv );
    8.  
    9.  
    10. MainWindow w;
    11. w.show();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by aurora; 7th May 2012 at 10:57.

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

    Default Re: custom table model is not working

    please somebody reply.....
    my final goal is to display very large text file (qround 1gb)...
    what should i do?
    will this model view helps me?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: custom table model is not working

    For one thing, you shouldn't be combining two QObject-derived classes in one pair of cpp and h files. Split them into one pair of cpp and h for MainWindow, and one pair for MulModel.

    For another, you haven't declared Q_OBJECT in your MulModel class so none of the signals and slots for the base class will work.

    For a third, in line 73 of mainwindow.cpp, you create a new QStandardItemModel, even though your MulModel class already *is* a model. You then fill this new model pointer up with stuff, and then let the pointer go out of scope when the function exits. How do you get at the contents of this dangling pointer to a model inside a model after that?

    And for a fourth, in line 172 of mainwindow.cpp, you create an instance of your MulModel class on the stack, proceed to fill it up with data, and then let it go out of scope (and get destroyed) when the method exits.

    So what does the table have to work with when you give it a pointer to an object that's about to be deleted, and that object has a dangling pointer that actually contains all the data but is completely inaccessible?

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: custom table model is not working

    You're making this overly complicated.

    Simply load the data from your file into a container, say QList<QStringList> LineList. Then in the model's data function return LineList[index.row()][index.column()].

    As d_stranz says, you need to create MulModel on the heap.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Replies: 2
    Last Post: 5th May 2012, 01:25
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  4. Replies: 0
    Last Post: 1st February 2010, 11:00
  5. Help with getting my custom model working
    By thomaspu in forum Qt Programming
    Replies: 19
    Last Post: 29th July 2007, 18:35

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.