Hi,
I want to display data in TableView which I am getting from XML file.
I wrote this code, but I have some error like this:
'QObject::QObject' : cannot access private member declared in class 'QObject' => error of dataobject.h
my code is correct?
main.cpp:
Qt Code:
  1. MyModel mymodel;
  2. engine.rootContext()->setContextProperty("mymodel", &mymodel);
To copy to clipboard, switch view to plain text mode 

main.qml:
Qt Code:
  1. TableView {
  2. id: tablemodel
  3. MyTableViewColumn {
  4. role: "pro1"
  5. title: "pro1"
  6. width: 70
  7. }
  8. ...
  9. ...
  10. MyTableViewColumn {
  11. role: "pro11"
  12. title: "pro11"
  13. width: 60
  14. }
  15. MyTableViewColumn {
  16. role: "index"
  17. title: " "
  18. width: 30
  19. }
  20. model: ListModel {
  21. id: myModel
  22. source:myModel.mydata
  23. }
  24. }
To copy to clipboard, switch view to plain text mode 

dataobject.h:
Qt Code:
  1. #ifndef DATAOBJECT_H
  2. #define DATAOBJECT_H
  3. #include <QObject>
  4. class DataObject: public QObject
  5. {
  6. Q_OBJECT
  7. Q_PROPERTY(QString newIndex READ getIndex WRITE setIndex NOTIFY indexChanged)
  8. Q_PROPERTY(QString newpro1 READ getpro1 WRITE setpro1 NOTIFY pro1Changed)
  9. ...
  10. ...
  11. Q_PROPERTY(QString newpro11 READ getpro11 WRITE setpro11 NOTIFY pro11Changed)
  12. Q_PROPERTY(QByteArray newImage READ getImage WRITE setImage NOTIFY imageChanged)
  13.  
  14.  
  15. public:
  16. DataObject();
  17.  
  18. Q_INVOKABLE QString getIndex() const;
  19. Q_INVOKABLE QString getpro1() const;
  20. ...
  21. ...
  22. Q_INVOKABLE QString getpro11() const;
  23. Q_INVOKABLE QByteArray getImage() const;
  24.  
  25. public slots:
  26.  
  27. void setIndex(QString index);
  28. void setpro1(QString pro1);
  29. ..
  30. ...
  31. void setpro11(QString pro11);
  32. void setImage(QByteArray image);
  33.  
  34. signals:
  35. void indexChanged(QString);
  36. void pro1Changed(QString);
  37. ...
  38. ...
  39. void pro11Changed(QString);
  40. void imageChanged(QByteArray);
  41.  
  42. private:
  43.  
  44. QString newIndex;
  45. QString newpro1;
  46. ...
  47. ...
  48. QString newpro11
  49. QByteArray newImage;
  50. };
  51.  
  52. #endif // DATAOBJECT_H
To copy to clipboard, switch view to plain text mode 

dataobject.cpp:
Qt Code:
  1. #include "dataobject.h"
  2.  
  3. DataObject::DataObject()
  4. {
  5. }
  6. DataObject::DataObject(QString index,QString pro1,.......,QString pro11)
  7. {
  8. newpro1=pro1;
  9. ...
  10. ...
  11. newpro11=pro11;
  12. }
  13.  
  14. QString DataObject::getpro1() const
  15. {
  16. return newpro1;
  17. }
  18. void DataObject::setpro1(QString pro1)
  19. {
  20. if (pro1 != newpro1)
  21. {
  22. newpro1= pro1;
  23. emit pro11Changed(pro1);
  24. }
  25. }
  26.  
  27. ...
  28. ...
  29.  
  30. QString DataObject::getpro11() const
  31. {
  32. return newpro11;
  33. }
  34. void DataObject::setpro11(QString pro11)
  35. {
  36. if (pro11 != newpro11)
  37. {
  38. newpro11= pro11;
  39. emit pro11Changed(pro11);
  40. }
  41. }
  42.  
  43. QByteArray DataObject::getImage() const
  44. {
  45. return newImage;
  46. }
  47. void DataObject::setImage(QByteArray image)
  48. {
  49. if (image != newImage)
  50. {
  51. newImage= image;
  52. emit imageChanged(image);
  53. }
  54. }
To copy to clipboard, switch view to plain text mode 

mymodel.h:
Qt Code:
  1. #ifndef MYMODEL_H
  2. #define MYMODEL_H
  3.  
  4. #include <QAbstractListModel>
  5. #include <dataobject.h>
  6.  
  7. class MyModel : public QAbstractTableModel
  8. {
  9. Q_OBJECT
  10. std::vector<DataObject> mydata;
  11. public:
  12. typedef std::vector<DataObject>::const_iterator const_iterator;
  13. explicit MyModel(QObject *parent = 0);
  14. enum {index,hardness,fromFirst,avgDiam,omgheAsar,firstDiam,secondDiam,timeNiro,niro,method,zoom,MAX_COLS};
  15.  
  16. int rowCount(const QModelIndex &parent) const;
  17. int columnCount(const QModelIndex &parent) const;
  18. QVariant data(const QModelIndex &index, int role) const;
  19. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  20. void addData(DataObject data);
  21. void removeData(int row);
  22. bool setData(const QModelIndex &index, const QVariant &value, int role);
  23. Qt::ItemFlags flags(const QModelIndex &index) const;
  24. DataObject& getData(size_t index);
  25. const_iterator begin()const{return mydata.begin();}
  26. const_iterator end()const{return mydata.end();}
  27. };
  28.  
  29.  
  30. #endif // MYMODEL_H
To copy to clipboard, switch view to plain text mode 

mymodel.cpp

Qt Code:
  1. #include "mymodel.h"
  2.  
  3. QVariant MyModel::data(const QModelIndex &index, int role) const
  4. {
  5. if(!index.isValid())
  6. return QVariant();
  7.  
  8. if(index.row() >= mydata.size() || index.row() < 0)
  9. return QVariant();
  10.  
  11. if(role == Qt::DisplayRole || role == Qt::EditRole)
  12. {
  13. switch(index.column())
  14. {
  15. case prop1:
  16. return mydata[index.row()].getprop1();
  17. ...
  18. ...
  19. case prop11:
  20. return mydata[index.row()].getprop11();
  21. }
  22. }
  23. return QVariant();
  24. }
  25. QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
  26. {
  27. if(role != Qt::DisplayRole)
  28. return QVariant();
  29.  
  30. if (orientation == Qt::Horizontal)
  31. {
  32. switch (section)
  33. {
  34.  
  35. case 1:
  36. return tr("pro1");
  37. ...
  38. ...
  39. case 9:
  40. return tr("pro11");
  41. }
  42. }
  43. return QVariant();
  44. }
  45. bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
  46. {
  47. if (index.isValid() && role == Qt::EditRole && !(index.row() >= mydata.size() || index.row() < 0))
  48. {
  49. int row = index.row();
  50.  
  51. switch(index.column())
  52. {
  53. case 0:
  54. mydata[row].setIndex(value.toString());
  55. break;
  56. case 1:
  57. mydata[row].setpro1(value.toString());
  58. break;
  59. ...
  60. ...
  61. case 11:
  62. mydata[row].setprop11(value.toString());
  63. break;
  64. default:
  65. return false;
  66. }
  67. emit dataChanged(index, index);
  68. return true;
  69. }
  70. return false;
  71. }
  72. Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
  73. {
  74. if (!index.isValid())
  75. return Qt::ItemIsEnabled;
  76. return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
  77. }
  78. void MyModel::addData(DataObject data)
  79. {
  80. if(std::find(mydata.begin(),mydata.end(),data)!=mydata.end())
  81. return;
  82. beginInsertRows(QModelIndex(),mydata.size(),mydata.size());
  83. /*BOOST_SCOPE_EXIT(this_){
  84.   this_->endInsertRows();
  85.   }BOOST_SCOPE_EXIT_END*/
  86. mydata.push_back(std::move(data));
  87. endInsertRows();
  88. }
  89. void MyModel::removeData(int row)
  90. {
  91. beginRemoveRows(QModelIndex(),row,row);
  92. /*BOOST_SCOPE_EXIT(this_){
  93.   this_->endRemoveRows();
  94.   }BOOST_SCOPE_EXIT_END//*/
  95. mydata.erase(std::next(mydata.begin(),row));
  96. //endRemoveRows();
  97. }
To copy to clipboard, switch view to plain text mode 

myxml.xpp:
Qt Code:
  1. void MyXML::saveXMLFile()
  2. {
  3. QFile file(newFileName);
  4. file.open(QIODevice::WriteOnly);
  5.  
  6. QXmlStreamWriter xmlWriter(&file);
  7. xmlWriter.setAutoFormatting(true);
  8. xmlWriter.writeStartDocument();
  9.  
  10. xmlWriter.writeStartElement("NewDataSet");
  11.  
  12. xmlWriter.writeStartElement("header");
  13. ...
  14. ...
  15. xmlWriter.writeEndElement();
  16.  
  17. MyModel model;
  18. MyModel::const_iterator it = model.begin(),end = model.end();
  19. for(;it != end;++it)
  20. {
  21. xmlWriter.writeStartElement("record");
  22.  
  23. // QFile fileImg(newImage);
  24. // fileImg.open(QIODevice::ReadOnly);
  25.  
  26. // QByteArray imageData = fileImg.readAll();
  27. // QByteArray imageData_Base64 = imageData.toBase64();
  28.  
  29. // xmlWriter.writeAttribute("image",imageData_Base64);
  30.  
  31. xmlWriter.writeTextElement("index",it->getIndex());
  32. xmlWriter.writeTextElement("prop1",it->getprop1());
  33. ...
  34. ...
  35. xmlWriter.writeTextElement("prop11",it->getprop11());
  36.  
  37. xmlWriter.writeEndElement();
  38. }
  39.  
  40. xmlWriter.writeEndElement();
  41.  
  42. file.close();
  43. }
  44.  
  45. void MyXML::readXMLFile()
  46. {
  47. QFile file(newFileName);
  48. if (!file.open(QFile::ReadOnly | QFile::Text))
  49. {
  50. //qDebug() << "Error: Cannot read file ";
  51. return;
  52. }
  53. QXmlStreamReader reader(file.readAll());
  54. file.close();
  55.  
  56. while(!reader.atEnd()) {
  57. reader.readNext();
  58. if (reader.isStartElement()) {
  59. if (reader.name() == "header") {
  60.  
  61. foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
  62. ...
  63. ...
  64. }
  65. }
  66. else if (reader.name() == "recorde") {
  67.  
  68. QString prop1,...,prop11;
  69.  
  70. foreach(const QXmlStreamAttribute &attr, reader.attributes()){
  71. if(attr.name().toString() == QLatin1String("index")){
  72. index=attr.value().toString();
  73. }
  74. else if(attr.name().toString() == QLatin1String("prop1")){
  75. prop1=attr.value().toString();
  76. }
  77.  
  78. ...
  79. ...
  80.  
  81. else if(attr.name().toString() == QLatin1String("prop11")){
  82. prop11=attr.value().toString();
  83. }
  84.  
  85. }
  86. MyModel model;
  87. model.addData(DataObject(index,prop1,....,prop11));
  88. }
  89. }
  90. }
  91. }
To copy to clipboard, switch view to plain text mode