Results 1 to 5 of 5

Thread: QBstractTableMode derived model, not showing in QTableView

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QBstractTableMode derived model, not showing in QTableView

    Hi,

    I managed to avoid using the Model/View framework for a long time, but now I have to.
    My problem is, that my model is not showing up in the QTableView.

    I have a QAbstractTableMode subclass, in which all the virtual function that have to be implemented are implemented:
    Qt Code:
    1. //QAbstractTableModel Virtuals
    2. virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    3. virtual bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
    4. virtual bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
    5. virtual bool setData ( const QModelIndex & index, const QVariant & value, int role );
    6. virtual QModelIndex index ( int row, int column, const QModelIndex & parent) const;
    7. virtual QModelIndex parent ( const QModelIndex & index ) const;
    8. virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
    To copy to clipboard, switch view to plain text mode 

    I then set my data model so:
    Qt Code:
    1. QTableView *pTable = new QTableView();
    2. pTable->setModel((QAbstractTableModel *)&m_logFile->dataModel());
    3. pTable->show();
    To copy to clipboard, switch view to plain text mode 

    but the table comes up empty.

    I can test that my model contains the correct data with the following code:
    Qt Code:
    1. //Log file returns a vector of vectors which desctribe a table.
    2. //The heart of my QAbstractTableModel has also the same vector of vectors.
    3. QVector<QVector<float> > data = m_logFile->data();
    4.  
    5. float f1,f2;
    6.  
    7. // f1 is the data from the above 'data' vector and f2 is the data from the QAbstractTableModel subclass.
    8. //and they all match. (all the items in their respective row/col.
    9. for(int iCol=0; iCol < data.size(); iCol++){
    10. for(int iRow=0; iRow < data.at(iCol).size(); iRow++){
    11. f1 = data.at(iCol).at(iRow);
    12. QModelIndex index = m_logFile->dataModel().index(iRow,iCol,QModelIndex());
    13. f2 = m_logFile->dataModel().data(index,Qt::DisplayRole).toFloat();
    14. }
    To copy to clipboard, switch view to plain text mode 

    What I also find interesting is, that after I set the model, my models headerData() gets called by the QTableView, but data() does not.
    Still, even the table headers are empty when it shows up.

    My guess is, that I am not doing something which I have to do (which I missed in the docs), or probably, doing something wrong, and would appreciate if you could help me to figure it out.
    Maybe if you can point out things that are common mistakes that I can check to see if I am making.

    I didn't implement a delegate yet, as I don't want to interact with my Table yet, at the moment I just want to see my data in the table.
    Let me know if you need to see implementation of methods.

    Thanks in advance.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QBstractTableMode derived model, not showing in QTableView

    Quote Originally Posted by high_flyer View Post
    Hi,

    I managed to avoid using the Model/View framework for a long time, but now I have to.
    My problem is, that my model is not showing up in the QTableView.

    I have a QAbstractTableMode subclass, in which all the virtual function that have to be implemented are implemented:
    Qt Code:
    1. //QAbstractTableModel Virtuals
    2. virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    3. virtual bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
    4. virtual bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
    5. virtual bool setData ( const QModelIndex & index, const QVariant & value, int role );
    6. virtual QModelIndex index ( int row, int column, const QModelIndex & parent) const;
    7. virtual QModelIndex parent ( const QModelIndex & index ) const;
    8. virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
    To copy to clipboard, switch view to plain text mode 

    I then set my data model so:
    Qt Code:
    1. QTableView *pTable = new QTableView();
    2. pTable->setModel((QAbstractTableModel *)&m_logFile->dataModel());
    3. pTable->show();
    To copy to clipboard, switch view to plain text mode 

    but the table comes up empty.

    I can test that my model contains the correct data with the following code:
    Qt Code:
    1. //Log file returns a vector of vectors which desctribe a table.
    2. //The heart of my QAbstractTableModel has also the same vector of vectors.
    3. QVector<QVector<float> > data = m_logFile->data();
    4.  
    5. float f1,f2;
    6.  
    7. // f1 is the data from the above 'data' vector and f2 is the data from the QAbstractTableModel subclass.
    8. //and they all match. (all the items in their respective row/col.
    9. for(int iCol=0; iCol < data.size(); iCol++){
    10. for(int iRow=0; iRow < data.at(iCol).size(); iRow++){
    11. f1 = data.at(iCol).at(iRow);
    12. QModelIndex index = m_logFile->dataModel().index(iRow,iCol,QModelIndex());
    13. f2 = m_logFile->dataModel().data(index,Qt::DisplayRole).toFloat();
    14. }
    To copy to clipboard, switch view to plain text mode 

    What I also find interesting is, that after I set the model, my models headerData() gets called by the QTableView, but data() does not.
    Still, even the table headers are empty when it shows up.

    My guess is, that I am not doing something which I have to do (which I missed in the docs), or probably, doing something wrong, and would appreciate if you could help me to figure it out.
    Maybe if you can point out things that are common mistakes that I can check to see if I am making.

    I didn't implement a delegate yet, as I don't want to interact with my Table yet, at the moment I just want to see my data in the table.
    Let me know if you need to see implementation of methods.

    Thanks in advance.
    First off, see if having an actual pointer to your model makes a difference instead of allocating memory for it in the setModel method; i.e.:
    QAbstractTableModelsubclass* myModel; //defined in header file as member of possibly QMainWindow
    myModel = new QQAbstractTableModelsubclass(...);
    pTable->setModel(myModel);

    I'm not exactly sure what is happening when you make the call to setModel with your parameters (not seeing the whole code), but it looks suspicious.

    Also, another avenue is seeing your data function, which I don't see listed in your header. This is where the table view actually gets the data for your model.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QBstractTableMode derived model, not showing in QTableView

    Hi,

    thanks for the reply.
    First off, see if having an actual pointer to your model makes a difference instead of allocating memory for it in the setModel method; i.e.:
    QAbstractTableModelsubclass* myModel; //defined in header file as member of possibly QMainWindow
    myModel = new QQAbstractTableModelsubclass(...);
    pTable->setModel(myModel);
    I am not sure what do you mean.
    The call:
    Qt Code:
    1. pTable->setModel((QAbstractTableModel *)&m_logFile->dataModel());
    To copy to clipboard, switch view to plain text mode 
    is not allocating anything.
    m_logFile->dataModel() returns a reference to a model which is allocated and lives in m_logFile.
    At the time when I am calling pTable->setModel() the model is well allocated and filled with data, so I am not sure what you might have meant there, can you restate maybe?

    I'm not exactly sure what is happening when you make the call to setModel with your parameters (not seeing the whole code), but it looks suspicious.
    Which setModel() do you mean? my model subclass setModel() or the pTable->setModel()?
    And what exactly you find suspicious - so I can check it.

    Also, another avenue is seeing your data function, which I don't see listed in your header. This is where the table view actually gets the data for your model.
    True.
    I forgot to add it here, I will later when I am home again. (don't have the code here).
    However, as I said, my data() function is not being called by the QTableView... all though I checked to make sure my virtual implementations signatures are correct, I will have a look again.

    Thanks, and if you have more pointers, please share.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QBstractTableMode derived model, not showing in QTableView

    I guess what I'm saying is that it is hard to see what you are trying to do with your model. Posting the code will be helpful to evaluate why you are not seeing the model data in the table view. Otherwise, I can only guess why it's not working.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QBstractTableMode derived model, not showing in QTableView

    Hi,

    I just ran my code on a different machine, and it works perfectly.
    No idea why on my machine it doesn't show up.

    Oh well.

    Thanks any way!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Problem showing QTableView
    By weaver4 in forum Qt Programming
    Replies: 6
    Last Post: 23rd November 2009, 13:42
  2. Replies: 4
    Last Post: 7th August 2009, 21:09
  3. QTableView only showing data when row selected
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2009, 05:34
  4. QTableView showing empty fields
    By ederbs in forum Qt Programming
    Replies: 5
    Last Post: 4th October 2007, 00:30
  5. QTableView not corectly showing empty fields
    By Sergey B. in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2007, 08:18

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.