Results 1 to 3 of 3

Thread: implementation of QabstractItemModel problem

  1. #1
    Join Date
    Aug 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default implementation of QabstractItemModel problem

    Hi,
    I'm using qt4 from few weeks.
    I've read the model subclassing reference page, and I've wrote my model.
    The problem is that the compiler says: "cannot declare variable ‘model’ to be of abstract type ‘DMapLayerModel" on this instruction
    Qt Code:
    1. DMapLayerModel model(obj)
    To copy to clipboard, switch view to plain text mode 

    The file DMapLayerModel.h is:

    Qt Code:
    1. #ifndef DMAPLAYERMODEL_H_
    2. #define DMAPLAYERMODEL_H_
    3.  
    4. #include <QtCore>
    5. #include <QtGui>
    6. #include "DMapLayerObject.h"
    7.  
    8. class DMapLayerModel : public QAbstractItemModel{
    9. Q_OBJECT
    10. public:
    11. DMapLayerModel(DMapLayerObject *layers, QObject *parent = 0);
    12. virtual ~DMapLayerModel();
    13. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    14. QVariant data(const QModelIndex &index, int role) const;
    15. QVariant headerData(int section, Qt::Orientation orientation,
    16. int role = Qt::DisplayRole) const;
    17. Qt::ItemFlags flags(const QModelIndex &index) const;
    18. bool setData(const QModelIndex &index, const QVariant &value,
    19. int role = Qt::EditRole);
    20. bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
    21. bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
    22.  
    23. private:
    24. DMapLayerObject *layers;
    25.  
    26. };
    27.  
    28. #endif /* DMAPLAYERMODEL_H_ */
    To copy to clipboard, switch view to plain text mode 

    and the DMapLayerModel.cpp is:

    Qt Code:
    1. #include "DMapLayerModel.h"
    2.  
    3. DMapLayerModel::DMapLayerModel(DMapLayerObject *layers, QObject *parent)
    4. : QAbstractItemModel(parent), layers(layers){
    5. }
    6.  
    7.  
    8. DMapLayerModel::~DMapLayerModel(){
    9.  
    10. }
    11.  
    12. int DMapLayerModel::rowCount(const QModelIndex &parent) const{
    13. int count=layers->getLayersNameList()->count();
    14. return count;
    15. }
    16.  
    17. QVariant DMapLayerModel::data(const QModelIndex &index, int role) const{
    18. if (!index.isValid())
    19. return QVariant();
    20.  
    21. if (index.row() >=layers->getLayersNameList()->size())
    22. return QVariant();
    23.  
    24. if (role == Qt::DisplayRole)
    25. return QVariant::fromValue(layers->getLayersNameList()->at(index.row()));
    26. else
    27. return QVariant();
    28.  
    29. }
    30.  
    31. QVariant DMapLayerModel::headerData(int section, Qt::Orientation orientation,
    32. int role) const{
    33.  
    34. if (role != Qt::DisplayRole)
    35. return QVariant();
    36.  
    37. if (orientation == Qt::Horizontal)
    38. return layers->getName();
    39. else
    40. return layers->getName();
    41. }
    42.  
    43. Qt::ItemFlags DMapLayerModel::flags(const QModelIndex &index) const{
    44.  
    45. if (!index.isValid())
    46. return Qt::ItemIsEnabled;
    47.  
    48. return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    49.  
    50. }
    51.  
    52. bool DMapLayerModel::setData(const QModelIndex &index, const QVariant &value,
    53. int role){
    54.  
    55. if (index.isValid() && role == Qt::EditRole) {
    56. layers->getLayersNameList()->replace(index.row(), value.toString());
    57. emit dataChanged(index, index);
    58. return true;
    59. }
    60. return false;
    61. }
    62.  
    63. bool DMapLayerModel::insertRows(int position, int rows, const QModelIndex &index){
    64. return true;
    65. }
    66.  
    67. bool DMapLayerModel::removeRows(int position, int rows, const QModelIndex &index){
    68. return true;
    69. }
    To copy to clipboard, switch view to plain text mode 

    I'm a bit confused.
    Thanks in advance.
    M.

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: implementation of QabstractItemModel problem

    That error means that you've haven't implemented a pure virtual function. Pure virtual functions are identified by having "= 0" in the prototype (as seen in the documentation). In this case the missing function is columnCount. If your model is a simple string list you may be interested in QStringListModel. You may also want to use Model Test with your custom model.

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

    mikelantonio (7th August 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: implementation of QabstractItemModel problem

    thanks, it works.
    I need to implement, other than columnCount, either parent and index methods.

Similar Threads

  1. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50
  2. problem with opengl, zooming, drawpixels, and origin
    By ntp in forum General Programming
    Replies: 0
    Last Post: 22nd February 2008, 21:48
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.