Results 1 to 2 of 2

Thread: Table Model / View Problem -- Data Not Displaying

  1. #1

    Default Table Model / View Problem -- Data Not Displaying

    Hello everyone,

    I'm having a difficult time getting a table set up. I subclassed QAbstractTableModel as such:
    Qt Code:
    1. int FileModel::rowCount(const QModelIndex &parent) const {
    2.  
    3. return files.count();
    4. }
    5. int FileModel::columnCount(const QModelIndex &parent) const {
    6. return 4;
    7. }
    8. QVariant FileModel::data(const QModelIndex &index, int role) const {
    9. printf("row %d\n",(int)index.row());
    10. if (!index.isValid())
    11. return QVariant();
    12.  
    13. if (index.row() >= files.size())
    14. return QVariant();
    15.  
    16. if (role == Qt::DisplayRole) {
    17. printf("%s\n",files.value(file_list.at(index.row()))->m_path.c_str());
    18. return QString(files.value(file_list.at(index.row()))->m_path.c_str());
    19. } else {
    20. return QVariant();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    I set the model in a view like this:
    Qt Code:
    1. FileModel fmodel;
    2. QTableView *table = new QTableView(this);
    3. table->setModel(&fmodel);
    4. table->setAlternatingRowColors(true);
    5. table->show();
    To copy to clipboard, switch view to plain text mode 

    However, an empty table shows up with the correct number of rows and columns. I'm not sure why this is happening and I've tried just about everything I can think of.

    Any ideas?

    Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Table Model / View Problem -- Data Not Displaying

    The model gets automatically destroyed when going out of scope. Allocate it on the heap:
    Qt Code:
    1. FileModel* fmodel = new FileModel(this);
    2. QTableView *table = new QTableView(this);
    3. table->setModel(fmodel);
    To copy to clipboard, switch view to plain text mode 
    PS. Have you noticed QDirModel?
    J-P Nurmi

Similar Threads

  1. Relational Table Model Problem
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2007, 14:57
  2. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  3. Replies: 6
    Last Post: 20th April 2006, 10:23
  4. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 20:59
  5. Table model / view editing issue
    By Caius Aérobus in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2006, 11:03

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.