Results 1 to 20 of 61

Thread: using an isntance of QSqlDatabase for connection defiinition

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Jun 2016
    Posts
    99
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: using an isntance of QSqlDatabase for connection defiinition

    Quote Originally Posted by anda_skoa View Post
    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 ?

    Qt Code:
    1. 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) ?
    Qt Code:
    1. //---Instance of userEventLogMsg Class Struct---//
    2. userEventLogMsg msg;
    3.  
    4. while (selectQuery.next()){
    5. //---Add query data to the userEventLogMsg class struct---//
    6. msg.id = selectQuery.value(0).toString();
    7. msg.username = selectQuery.value(1).toString();
    8. msg.eventmessage = selectQuery.value(2).toString();
    9. msg.datetime = selectQuery.value(3).toString();
    10. //---Use model object to access an call addEvent()---//
    11. addEvent(msg);
    12. }
    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?)

    Qt Code:
    1. mUserEventLogModel = new UserEventLog();
    2. m_QmlEngine->rootContext()->setContextProperty("UserEventLog", mUserEventLogModel);
    To copy to clipboard, switch view to plain text mode 

    In my QML I call my funtions

    Qt Code:
    1. Action {
    2. id: action_userEventLogBtn
    3. enabled:!inSequence
    4. onTriggered:{
    5. //----Code to Load User Event Database into tableView-----//
    6. input_loader.filename = ""
    7. UserEventLog.dbConnect();
    8. UserEventLog.sqlSelect();
    9. weld_view.state = "USEREVENT"
    10. 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.
    Last edited by jfinn88; 25th August 2016 at 19:37.

Similar Threads

  1. Replies: 16
    Last Post: 4th September 2013, 00:49
  2. How to set x509 on a QSqlDatabase Connection?
    By m3rlin in forum Qt Programming
    Replies: 24
    Last Post: 21st February 2012, 04:04
  3. Windows OCI QSqlDatabase connection
    By hollyberry in forum Newbie
    Replies: 10
    Last Post: 13th February 2012, 22:13
  4. QSqlDatabase Connection Close on Destruction
    By Sanuden in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2011, 15:32
  5. QSqlDatabase connection timeout?
    By joseprl89 in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2011, 01:43

Tags for this Thread

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.