Database Connection Not Open When Calling From Another Window



  • Okay, so I am attempting to override a close event to query my database to update the MainWindow with the changes that were made while Dialog window was open, however when calling my updateWindow() from Car, the database is not open. What I thought was needed is public.. Could be something stupid I'm overlooking as I am certainly no expert, but a tiny explanation why it's not working would be very helpful.

    car.h

    void Car::closeEvent(QCloseEvent *event)
    {
        MainWindow mw;
        mw.updateWindow(); //Calling to hopefully use function in MainWindow
    }
    

    mainwindow.h

    public:
        QSqlDatabase db;
        QString aPath = qApp->applicationDirPath();
        QString dPath = aPath + "/RacingInfo.sqlite";
        QSqlQuery *query = new QSqlQuery(db);
    
    
        void updateWindow()
        {
            QSqlQuery *query = new QSqlQuery(db);
    
            QSqlQueryModel * modal=new QSqlQueryModel();
            query->prepare("SELECT type || ' ' || number FROM Car WHERE rowid >= 1");
            query->exec();
            modal->setQuery(*query);
            ui->carSelect->setModel(modal);
    
            QSqlQueryModel * carModal=new QSqlQueryModel();
            QSqlQuery* carQuery=new QSqlQuery();
            carQuery->prepare("SELECT name FROM Track WHERE rowid >= 1");
            carQuery->exec();
            carModal->setQuery(*carQuery);
            ui->trackSelect->setModel(carModal);
    
            QSqlQueryModel * racetypeModal=new QSqlQueryModel();
            QSqlQuery* racetypeQuery=new QSqlQuery();
            racetypeQuery->prepare("SELECT type FROM RaceType WHERE rowid >= 1");
            racetypeQuery->exec();
            racetypeModal->setQuery(*racetypeQuery);
            ui->raceSelect->setModel(racetypeModal);
        }
    

  • Moderators

    @andrewhopps

    Where do you open the database?
    You declare QSqlDatabase. Then you seem to define the actual name of the db, but you are not opening it.

    BTW I would not place the whole functionality in the class definition.



  • you never call db.open() so why would you expect it to be open?

    Also this is not great, as variable initialisation should be done in the constructor

    QSqlDatabase db;
        QString aPath = qApp->applicationDirPath();
        QString dPath = aPath + "/RacingInfo.sqlite";
        QSqlQuery *query = new QSqlQuery(db);
    

    P.S.
    I'm not sure you are that you grasped scoping and life-cycle of variables in full:

    MainWindow mw;
        mw.updateWindow();
    

    This creates a new window that is never displayed, updates it and then destroys it

    QSqlQuery *query = new QSqlQuery(db);
    

    this creates a new query shadowing the one in the members



  • I've tried placing db.open() inside and I still have the same result with it not being open, so I didn't include in the code.

    @VRonin Definitely just trying to wing it currently, but will surely take into account what you've said.

    ** The database is open at all other times with various queries at initial opening of main, and of car, but when trying to update upon closing car with the update function, it doesnt work. Every other time it does, and thats where im lost.



  • @andrewhopps said in Database Connection Not Open When Calling From Another Window:

    queries at initial opening of main

    MainWindow mw;
    mw.updateWindow();
    This creates a new window that is never displayed, updates it and then destroys it

    it's not the same object, how do you connect to the database?



  • @VRonin Okay, possibly following along now. So in simple talk, I am creating an imaginary object and trying to use a function of that imaginary thing and not actually calling from my MainWindow?

    ** Seems to be bad practice and how i've been trying to open database in every instance, but through probably over-coding, I've made things work.... Could you point me in the right direction for proper calling of these functions then...


  • Qt Champions 2016

    Hi
    When car is open. There is already an instance of your mainwindow. ?
    So the easy way would be to use a signal and slot

    so signal in car and slot in mainwindow
    connect car signal to mainwindow

    and in car
    void Car::closeEvent(QCloseEvent *event)
    {
    emit UpdateDB(); <<< the new car signal.
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.