Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Database Connection Not Open When Calling From Another Window

Database Connection Not Open When Calling From Another Window

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andrewhopps
    wrote on last edited by
    #1

    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);
        }
    
    K 1 Reply Last reply
    0
    • A andrewhopps

      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);
          }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • A Offline
          A Offline
          andrewhopps
          wrote on last edited by andrewhopps
          #4

          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.

          VRoninV 1 Reply Last reply
          0
          • A andrewhopps

            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.

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @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?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            A 1 Reply Last reply
            2
            • VRoninV VRonin

              @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?

              A Offline
              A Offline
              andrewhopps
              wrote on last edited by andrewhopps
              #6

              @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...

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                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.
                }

                1 Reply Last reply
                3

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved