Originally Posted by
anda_skoa
No, I would leave the QSqlQuery outside.
Okay, That makes sense played with it and I declare it before the if() block.
No, you should be able to keep more than one connection at any given time.
Okay, so I'm fine with leaving conn1, and conn2 connected.
You likely want to use the m_selectDataBase for this query, no?
I can pass it with the sql statement correct ?
QSqlQuery selectQuery
("SELECT id, userName, eventMessage, dateTime FROM userlogevents", m_selectDataBase
);
QSqlQuery selectQuery("SELECT id, userName, eventMessage, dateTime FROM userlogevents", m_selectDataBase);
To copy to clipboard, switch view to plain text mode
This doesn't look at all what I assume you want.
This creates a new model instance in every loop iteration, adds one record and then destroys the model (when it goes out of scope).
you are correct I do not want to create a new object every time it goes threw while loop moved object declaration above while loop. Will calling addEvent () in the while loop be fine or should it be called once after the while loop is done putting data in struct?
I’m still little confused how the virtual functions get called... from my understanding is when the instance variable of my class is created (to pass the model to C++ by setting rootContext item) it makes a call to the constructor and when the constructor gets called the virtual functions get called (trying to clear any confuse I still have with models and virtual functions) ?
//---Instance of userEventLogMsg Class Struct---//
userEventLogMsg msg;
while (selectQuery.next()){
//---Add query data to the userEventLogMsg class struct---//
msg.id = selectQuery.value(0).toString();
msg.username = selectQuery.value(1).toString();
msg.eventmessage = selectQuery.value(2).toString();
msg.datetime = selectQuery.value(3).toString();
//---Use model object to access an call addEvent()---//
addEvent(msg);
}
//---Instance of userEventLogMsg Class Struct---//
userEventLogMsg msg;
while (selectQuery.next()){
//---Add query data to the userEventLogMsg class struct---//
msg.id = selectQuery.value(0).toString();
msg.username = selectQuery.value(1).toString();
msg.eventmessage = selectQuery.value(2).toString();
msg.datetime = selectQuery.value(3).toString();
//---Use model object to access an call addEvent()---//
addEvent(msg);
}
To copy to clipboard, switch view to plain text mode
Also I want to call dbConnect and selectQuery() methods from QML (button click)
in the header file I declare the function as Q_INVOKABLE now I know I could register the QML type to make the class accessible in QML and import it in the QML to call the function but I think I would have to make the class global object? is there a simpler way to do this ?
I have seen an example with Q_INVOKABLE and setting the context property, but I get confused on what engine object/class I need to use to set it. I don't quit understand the rootContext methodology yet and have a QML engine already defined and in use that I don't want to mess up, Im not sure if replacing what is currently on the engine stack is the right way todo it? or if using a new engine object is correct... would I need to create an instance object of a qml engine in my userEvetnLog class? (how can I tell what object this is pointing to?)
mUserEventLogModel = new UserEventLog();
m_QmlEngine->rootContext()->setContextProperty("UserEventLog", mUserEventLogModel);
mUserEventLogModel = new UserEventLog();
m_QmlEngine->rootContext()->setContextProperty("UserEventLog", mUserEventLogModel);
To copy to clipboard, switch view to plain text mode
In my QML I call my funtions
Action {
id: action_userEventLogBtn
enabled:!inSequence
onTriggered:{
//----Code to Load User Event Database into tableView-----//
input_loader.filename = ""
UserEventLog.dbConnect();
UserEventLog.sqlSelect();
weld_view.state = "USEREVENT"
onLoaded: console.log("User Event Log");
Action {
id: action_userEventLogBtn
enabled:!inSequence
onTriggered:{
//----Code to Load User Event Database into tableView-----//
input_loader.filename = ""
UserEventLog.dbConnect();
UserEventLog.sqlSelect();
weld_view.state = "USEREVENT"
onLoaded: console.log("User Event Log");
To copy to clipboard, switch view to plain text mode
update: Okay I got my functions working in QML used previously defined QQmlEngine and instance of UserEventLog class that was used to make my model available to QML
update: made a stupid mistake when calling my addEvent function and now just realized it. Was getting a segfault fixed by removing UserEventLog object and just calling addEvent since Im in the class.
Bookmarks