Results 1 to 3 of 3

Thread: QAbstractItemModel now showing data in QTableView

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question QAbstractItemModel now showing data in QTableView

    This is all Qt 4.8:

    There is a custom model, FileCopyItemModel, that implements the QAbstractItemModel to hold a list of files that need to be copied. The list of files and their properties are stored in another class (ImageCopyDTO) so QList<ImageCopyDTO> is the actual collection of data. When a QTableView's model is set to an instance of FileCopyItemModel, the columns get their names from headerData(), but no data is ever displayed. The QList<ImageCopyDTO> has elements, but the FileCopyItemModel::data() never gets called. Any idea why?

    FileCopyItemModel.h
    Qt Code:
    1. typedef QList<ImageCopyDTO> ImageCopyDTOList;
    2.  
    3. class FileCopyItemModel: public QAbstractItemModel
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8.  
    9. int relativePathNo() const { return 0; }
    10. int noCopyNo() const { return 1; }
    11. int imageName2CopyNo() const { return 2; }
    12. int filesCopyCntNo() const { return 3; }
    13. int qtyNo() const { return 4; }
    14. int productNameNo() const { return 5; }
    15. int notesNo() const { return 6; }
    16.  
    17. explicit FileCopyItemModel(QObject *parent = 0);
    18. ~FileCopyItemModel();
    19.  
    20. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const ;
    21. virtual QModelIndex parent(const QModelIndex &child) const;
    22. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    23. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
    24. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    25. bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
    26.  
    27. void clear() { _data.clear(); }
    28.  
    29. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    30. Qt::ItemFlags flags(const QModelIndex &index) const override;
    31.  
    32. // classic
    33. void append(const ImageCopyDTO& element) { _data.append(element); }
    34. ImageCopyDTOList getElementsFromData() const { return _data; }
    35.  
    36. private:
    37. ImageCopyDTOList _data;
    38. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. FileCopyItemModel::FileCopyItemModel(QObject *parent)
    2. {
    3. }
    4.  
    5. FileCopyItemModel::~FileCopyItemModel(void)
    6. {
    7. }
    8.  
    9. QModelIndex FileCopyItemModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const
    10. {
    11. return QModelIndex();
    12. }
    13.  
    14. QModelIndex FileCopyItemModel::parent(const QModelIndex &child) const
    15. {
    16. return QModelIndex();
    17. }
    18.  
    19. int FileCopyItemModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
    20. {
    21. return _data.count();
    22. }
    23.  
    24. int FileCopyItemModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
    25. {
    26. return 7;
    27. }
    28.  
    29. bool FileCopyItemModel::setData( const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole*/ )
    30. {
    31. bool result = false;
    32.  
    33. int row = index.row();
    34. int column = index.column();
    35.  
    36. if (!index.isValid() || row >= rowCount() || column >= columnCount())
    37. {
    38. return result;
    39. }
    40.  
    41. if(role == Qt::EditRole)
    42. {
    43. switch (column) {
    44. case 1:
    45. _data[row].setNoCopy(value.toBool());
    46. result = true;
    47. break;
    48. case 2:
    49. _data[row].setImageName2Copy(value.toString());
    50. result = true;
    51. break;
    52. case 3:
    53. _data[row].setFilesCopyCnt(value.toInt());
    54. result = true;
    55. break;
    56. default:
    57. break;
    58. } // end switch column
    59. }
    60.  
    61. return result;
    62. }
    63.  
    64. QVariant FileCopyItemModel::data(const QModelIndex &index, int role) const
    65. {
    66. QVariant result;
    67.  
    68. int row = index.row();
    69. int column = index.column();
    70.  
    71. if (!index.isValid() || row >= rowCount() || column >= columnCount())
    72. {
    73. return result;
    74. }
    75.  
    76. switch (role)
    77. {
    78. case Qt::DisplayRole:
    79. // checking row and column to get data
    80. switch (column) {
    81. case 0:
    82. result = _data.at(row).relativePath();
    83. break;
    84. case 1:
    85. result = _data.at(row).noCopy();
    86. break;
    87. case 2:
    88. result = _data.at(row).imageName2Copy();
    89. break;
    90. case 3:
    91. result = _data.at(row).filesCopyCnt();
    92. break;
    93. case 4:
    94. result = _data.at(row).qty();
    95. break;
    96. case 5:
    97. result = _data.at(row).productName();
    98. break;
    99. case 6:
    100. result = _data.at(row).notes();
    101. break;
    102. default:
    103. break;
    104. } // end switch column
    105. break;
    106. default:
    107. break;
    108. }
    109.  
    110. return result;
    111. }
    112.  
    113. QVariant FileCopyItemModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
    114. {
    115. QVariant result = QVariant();
    116.  
    117. if (Qt::DisplayRole == role && Qt::Horizontal == orientation)
    118. {
    119. switch (section)
    120. {
    121. case 0:
    122. result = "Copy";
    123. break;
    124. case 1:
    125. result = "Image Gallery";
    126. break;
    127. case 2:
    128. result = "Image";
    129. break;
    130. case 3:
    131. result = "File Count";
    132. break;
    133. case 4:
    134. result = "Item Qty";
    135. break;
    136. case 5:
    137. result = "Product";
    138. break;
    139. case 6:
    140. result = "Notes";
    141. break;
    142. default:
    143. break;
    144. }
    145. }
    146.  
    147. return result;
    148. }
    149.  
    150. Qt::ItemFlags FileCopyItemModel::flags(const QModelIndex &index) const
    151. {
    152. Qt::ItemFlags result = Qt::ItemIsEditable | QAbstractItemModel::flags(index);
    153. return result;
    154. }
    To copy to clipboard, switch view to plain text mode 

    Right now a pointer to the instance is being passed into the dialog with the QTableView. In the constructor this is being executed right now:

    Qt Code:
    1. ui.fileCopyTableView->setModel(_model);
    2. ui.fileCopyTableView->show();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,134
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QAbstractItemModel now showing data in QTableView

    but no data is ever displayed.
    Your index() method is returning a NULL QModelIndex, so in effect even though the model is returning valid row and column counts, whenever the table view asks the model for the index of the item at a particular row and column, the model is saying, "Sorry, that doesn't exist".

    Qt Code:
    1. QModelIndex FileCopyItemModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const
    2. {
    3. QModelIndex retVal;
    4. if ( !parent.isValid() ) // valid row, column combos don't have parents; you could also check to see if row and column are valid
    5. retVal = createIndex( row, column );
    6. return retVal;
    7. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    scarleton (23rd July 2022)

  4. #3
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractItemModel now showing data in QTableView

    Thank you, I knew it was something obvious I was missing... It has been a few years since I did Qt development

Similar Threads

  1. QTableView + QAbstractItemModel and adding rows
    By karlkar in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2016, 21:45
  2. Replies: 4
    Last Post: 18th May 2014, 14:24
  3. How to make QAbstractItemModel 's data checkable
    By nifei in forum Qt Programming
    Replies: 12
    Last Post: 1st April 2013, 19:52
  4. QTableView only showing data when row selected
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2009, 05:34
  5. Question on QAbstractItemModel vs. QTableView
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2007, 07:29

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.