Here is my code for a listView model. It compiles part way and then hangs. I'm not sure if I need to declare msg QList as pointer or not (sidebar: if I do would like to allocate memory on the heap, to do I use the keyword "new"?). It seems to hang on setting the context property in main.cpp when I run the debugger with break points. This all new to me so I'm still sorting out how everything works together in the model to put data in a list to display. My understanding is I create a model in C++ which must include the rowCount(), roleNames(), data() functions. Will also need an enum var to declare the roleNames to be used. Will also need some sort of data structure (struct) to store the data from the database using roleNames. I believe the roleNames have to be the same name as the column header in the sqlite database. I use a sql statement to select data from the database then I loop through the query and add it to the data struct. Use the data struct to populate the list. each row I pull from the database is an element of the list. Is this understanding correct? I think one thing I have trouble with is managing the pointer I'm using... Also little fuzzy on setting the root context (methodology) I know this is a lot but I'm learning and any help is appreciated.

background:
I need to create a user event log that monitors the activity of a user in an application (weld programming application).

I have created a new class to push user events to a database e.g. when they change settings or change weld program. (I have completed this part successfully).


now I need to display this information in a dialogue for viewing so that managers can monitor employee activity in the application.


Evidently I need to be able to filter by date and by user once I can get the display working. Also need to figure out how to create a new table every day and delete old tables after 30 days I figure I can create method to create tables and delete tables. Then is just pretty-up the display.
I have decided to use C++ model to display this and the application GUI is in QML so will need to display in QML I'm using Qt5.5


Qt Code:
  1. #ifndef SQLITEMODEL_H
  2. #define SQLITEMODEL_H
  3.  
  4. #include <assert.h>
  5. #include <list>
  6. #include <QList>
  7. #include <QColor>
  8. #include <QObject>
  9. #include <QDebug>
  10. #include <QString>
  11. #include <QFileInfo>
  12. #include <QDateTime>
  13. #include <QQmlError>
  14. #include <QQmlApplicationEngine>
  15. #include <QQmlEngine>
  16. #include <QQmlContext>
  17. #include <QtSql/QSqlDatabase>
  18. #include <QtSql/QSqlQuery>
  19. #include <QtSql/QSqlError>
  20. #include <QtSql/QSqlRecord>
  21. #include <QModelIndex>
  22. #include <QAbstractListModel>
  23.  
  24. struct userEventLogMsg{
  25. //hold all values for a single list entry,
  26. QString id;
  27. QString username;
  28. QString eventmessage;
  29. QDateTime datetime;
  30. };
  31.  
  32. class sqliteModel:public QAbstractListModel
  33. {
  34. Q_OBJECT
  35.  
  36. public:
  37. explicit sqliteModel(QObject *parent = 0);
  38.  
  39. ~sqliteModel();
  40.  
  41. enum userEventRoles {idRole, nameRole, msgRole, dateRole = Qt::UserRole + 220};
  42.  
  43. int rowCount(const QModelIndex & parent) const;
  44.  
  45. QHash<int, QByteArray> roleNames() const;
  46.  
  47. QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
  48.  
  49. void addEvent(const userEventLogMsg &msg);
  50.  
  51. private:
  52. QList<userEventLogMsg> m_msgList;
  53. };
  54.  
  55. #endif // SQLITEMODEL_H
To copy to clipboard, switch view to plain text mode 

=========================================== CPP FILE =================================================

Qt Code:
  1. #include "sqlitemodel.h"
  2.  
  3. sqliteModel::sqliteModel(QObject *parent):QAbstractListModel(parent)
  4. {
  5.  
  6. }
  7.  
  8. sqliteModel::~sqliteModel()
  9. {
  10.  
  11. }
  12.  
  13. int sqliteModel::rowCount(const QModelIndex &parent) const
  14. {
  15. Q_UNUSED(parent);
  16. return m_msgList.count();
  17. qDebug()<< m_msgList.count();
  18. }
  19.  
  20. QHash<int, QByteArray> sqliteModel::roleNames() const
  21. {
  22. QHash<int, QByteArray> roleNames;
  23. roleNames.insert(idRole, "id");
  24. roleNames.insert(nameRole, "userName");
  25. roleNames.insert(msgRole, "eventMessage");
  26. roleNames.insert(dateRole, "dateTime");
  27. qDebug()<< roleNames;
  28. return roleNames;
  29. }
  30.  
  31. QVariant sqliteModel::data(const QModelIndex &index, int role) const
  32. {
  33. if (index.row() < 0 || index.row() >= m_msgList.count())
  34. {
  35. return QVariant();
  36. qDebug() << index.row();
  37. }
  38.  
  39. QVariant text;
  40.  
  41. if(role == idRole)
  42. {
  43. userEventLogMsg msg = m_msgList.at(0);
  44. text = msg.id;
  45. qDebug() << text;
  46. }
  47. else if(role == nameRole)
  48. {
  49. userEventLogMsg msg = m_msgList.at(1);;
  50. text = msg.username;
  51. qDebug() << text;
  52. }
  53. else if(role == msgRole)
  54. {
  55. userEventLogMsg msg = m_msgList.at(2);;
  56. text = msg.eventmessage;
  57. qDebug() << text;
  58. }
  59. if(role == dateRole)
  60. {
  61. userEventLogMsg msg = m_msgList.at(3);
  62. text = msg.datetime.toLocalTime().toString("M'/'d'/'yyyy' 'h:mm:ss ap" );
  63. qDebug() << text;
  64. }
  65. qDebug() << text;
  66. return text;
  67. }
  68.  
  69. void sqliteModel::addEvent(const userEventLogMsg &msg)
  70. {
  71. qDebug()<<"made it inside addEvent 1";
  72. beginInsertRows(QModelIndex(), 0, 0);
  73. qDebug()<<"made it inside addEvent 2";
  74. //m_msgList->insert(0, msg);
  75. m_msgList.append(msg);
  76. //qDebug() << m_msgList;
  77.  
  78. endInsertRows();
  79. }
To copy to clipboard, switch view to plain text mode 
============================================ main.cpp ================================================

Qt Code:
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QSqlDatabase>
  4. #include "sqlitemodel.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QGuiApplication app(argc, argv);
  9.  
  10. sqliteModel *model = new sqliteModel;
  11.  
  12. db = QSqlDatabase::addDatabase("QSQLITE");
  13. db.setDatabaseName("/home/amet/git/rnd/userLog.db");
  14. db.open();
  15.  
  16. if(!db.open()){
  17. qDebug() <<"error in opening DB";
  18. }
  19. else{
  20. qDebug() <<"connected to DB" ;
  21. }
  22.  
  23. QSqlQuery myQuery("SELECT id, userName, eventMessage, dateTime FROM userlogevents");
  24.  
  25. if(myQuery.exec("SELECT id, userName, eventMessage, dateTime FROM userlogevents")){
  26. qDebug()<<"sql statement exicuted fine";
  27. }
  28. else{
  29. qDebug() <<"Errors accured with sql statement";
  30. qDebug() <<myQuery.lastError();
  31. }
  32.  
  33. while (myQuery.next()){
  34. userEventLogMsg *msg = new userEventLogMsg;
  35. QString myString = myQuery.value(0).toString();
  36. msg->id.insert(0, myString);
  37. //msg->username.append(myQuery.value(3).toString());
  38. //msg->eventmessage.append(myQuery.value(4).toString());
  39. //msg->datetime.append(myQuery.toLocalTime().toString("M'/'d'/'yyyy' 'h:mm:ss ap'"));
  40. model->addEvent(*msg);
  41. }
  42.  
  43. QQmlApplicationEngine engine;
  44. QQmlContext *contxt = engine.rootContext();
  45. contxt->setContextProperty("sqliteModel", model);
  46. return app.exec();
  47. }
To copy to clipboard, switch view to plain text mode 

============================================ main QML ================================================

Qt Code:
  1. import QtQuick 2.6
  2. import QtQuick.Layouts 1.1
  3. import QtQuick.Controls 1.3
  4. import QtQuick.Window 2.2
  5. import QtQuick.Dialogs 1.2
  6. import QtQuick.Layouts 1.1
  7. import QtQuick.Controls 2.0
  8. import Qt.labs.folderlistmodel 2.1
  9.  
  10. //---/ Display Rows from database in tableView /---//
  11. Window
  12. {
  13. ListView
  14. {
  15. width: 600; height: 250
  16. //---/ C++ model set using context property in main.cpp /---//
  17. model: sqliteModel
  18. //---// items are drawn by a delegate. /---//
  19. delegate: Text { text: "Record: " + id + ", " + ", " + userName + ", " + eventMessage + ", " + dateTime}
  20. }
  21. }
To copy to clipboard, switch view to plain text mode