Results 1 to 3 of 3

Thread: QAbstractTableModel not showing data

  1. #1
    Join Date
    Mar 2013
    Posts
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QAbstractTableModel not showing data

    I'm using QAbstractTableModel subclassing and I can access to the date but when I try to associate theese data to a QtableView model I don't see anything.

    This is mi code:

    .cpp
    Qt Code:
    1. #include "guiProductTable.h"
    2.  
    3. guiProductTable::guiProductTable(int numRows, int numColumns, QList<QMap<QString, QString> > productList)
    4. : m_numRows(numRows),
    5. m_numColumns(numColumns),
    6. products(productList)
    7. {
    8. }
    9.  
    10. int guiProductTable::rowCount(const QModelIndex& parent) const
    11. {
    12. return m_numRows;
    13. }
    14.  
    15. int guiProductTable::columnCount(const QModelIndex& parent) const
    16. {
    17. return m_numColumns;
    18. }
    19.  
    20. QVariant guiProductTable::data(const QModelIndex& index, int role) const
    21. {
    22. if (!index.isValid() || role != Qt::DisplayRole)
    23. return QVariant();
    24.  
    25. // Return the data to which index points.
    26.  
    27. switch (index.column()){
    28. case 0:
    29. return products.at(index.row())["name"];
    30. break;
    31. case 1:
    32. return products.at(index.row())["id"];
    33. break;
    34. case 2:
    35. return products.at(index.row())["price"];
    36. break;
    37. case 3:
    38. return products.at(index.row())["barcode"];
    39. break;
    40. default:
    41. return QVariant();
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    .h
    Qt Code:
    1. #ifndef GUIPRODUCTTABLE_H
    2. #define GUIPRODUCTTABLE_H
    3.  
    4. #include <QtGui>
    5. #include <QtCore>
    6. #include "guiProductTable.h"
    7.  
    8. class guiProductTable : public QAbstractTableModel
    9. {
    10. //Q_OBJECT
    11.  
    12. public:
    13. guiProductTable(int numRows, int numColumns, QList< QMap<QString,QString> > productList);
    14. int rowCount(const QModelIndex& parent = QModelIndex()) const;
    15. int columnCount(const QModelIndex& parent = QModelIndex()) const;
    16. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
    17. private:
    18. int m_numRows;
    19. int m_numColumns;
    20. QList< QMap<QString,QString> > products;
    21. };
    22.  
    23. #endif // GUIPRODUCTTABLE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. guiProductTable productTable(productList.count(),4,productList);
    2. ui->tableView->setModel(&productTable);
    3.  
    4. /* QVariant uno = productTable.data(productTable.index(0,0));
    5.   QVariant due = productTable.data(productTable.index(0,1));
    6.   QVariant tre = productTable.data(productTable.index(0,2));
    7.   QVariant quattro = productTable.data(productTable.index(0,3));
    8.   std::cout<<uno.toString().toStdString()<<due.toString().toStdString()<<tre.toString().toStdString()<<quattro.toString().toStdString();*/
    To copy to clipboard, switch view to plain text mode 

    if i remove the comment at the bottom I can see the right output.

    If i remove comment here: //Q_OBJECT i get a compiling error


    Added after 1 8 minutes:


    I'v founded the solution.

    I had to move the model from stack area to the heap.
    Last edited by rspock; 21st March 2013 at 23:56.

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractTableModel not showing data

    Or you can make it class member.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,394
    Thanks
    247
    Thanked 690 Times in 680 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QAbstractTableModel not showing data

    I'v founded the solution.

    I had to move the model from stack area to the heap.
    And do you understand why? (And why lanz's suggest will probably also work?)

    My guess is that at the same time you solved your problem in this way, you also created a memory leak. But it is hard to say for sure without seeing how you implemented your solution.

Similar Threads

  1. qabstracttablemodel/qtableview: vector data columns
    By xyfix in forum Qt Programming
    Replies: 0
    Last Post: 15th September 2010, 12:51
  2. Replies: 1
    Last Post: 20th May 2009, 20:36
  3. QAbstractTableModel , custom data
    By akon in forum Newbie
    Replies: 0
    Last Post: 17th April 2009, 16:03
  4. QAbstractTableModel data insert issues
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:16
  5. QAbstractTableModel::data not being called...
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 09:52

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.