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. Condensing sequential setQuery?

Condensing sequential setQuery?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 979 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

    All throughout my program, I have so much excess code from troubleshooting on and off since I've started messing with it. I have an update function that refreshes a few combo-boxes on my mainwindow. Such as:

       void updateWindow() //Fully Populates All Fields Properly
        {
    
            QSqlQueryModel * modal=new QSqlQueryModel;
            modal->setQuery("SELECT type || ' ' || number FROM Car WHERE rowid >= 1");
            ui->carSelect->setModel(modal);
    
            QSqlQueryModel * carModal=new QSqlQueryModel;
            carModal->setQuery("SELECT name FROM Track WHERE rowid >= 1");
            ui->trackSelect->setModel(carModal);
    
            QSqlQueryModel * racetypeModal=new QSqlQueryModel();
            racetypeModal->setQuery("SELECT type FROM RaceType WHERE rowid >= 1");
            ui->raceSelect->setModel(racetypeModal);
        }
    

    Can I condense this any further or eliminate the need for creating a model for each combo-box by iterating through? (Have tried nesting inside if statements, but using only one model just sets all to the last set model)

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

      eliminate the need for creating a model for each combo-box by iterating through?

      This is the primary problem, especially because you don't supply a parent to the model so you are leaking a lot of memory.

      In the private section of the class declare:

      QSqlQueryModel* modal = nullptr;
      QSqlQueryModel* carModal = nullptr;
      QSqlQueryModel * racetypeModal=nullptr;
      

      then use:

      void updateWindow() //Fully Populates All Fields Properly
          {
      
              if(!modal)  {modal=new QSqlQueryModel(this); ui->carSelect->setModel(carModal);}
              modal->setQuery("SELECT type || ' ' || number FROM Car WHERE rowid >= 1");
      
               if(!carModal)  {carModal=new QSqlQueryModel(this); ui->trackSelect->setModel(carModal);}
              carModal->setQuery("SELECT name FROM Track WHERE rowid >= 1");
              
      
             if(!racetypeModal){
      racetypeModal=new QSqlQueryModel(this);
      ui->raceSelect->setModel(racetypeModal);
      }
              racetypeModal->setQuery("SELECT type FROM RaceType WHERE rowid >= 1");
          }
      

      Can I condense this any further

      Unless there are meaningful relations between the tables (and given the use case I doubt there are), no

      "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
      3
      • VRoninV VRonin

        eliminate the need for creating a model for each combo-box by iterating through?

        This is the primary problem, especially because you don't supply a parent to the model so you are leaking a lot of memory.

        In the private section of the class declare:

        QSqlQueryModel* modal = nullptr;
        QSqlQueryModel* carModal = nullptr;
        QSqlQueryModel * racetypeModal=nullptr;
        

        then use:

        void updateWindow() //Fully Populates All Fields Properly
            {
        
                if(!modal)  {modal=new QSqlQueryModel(this); ui->carSelect->setModel(carModal);}
                modal->setQuery("SELECT type || ' ' || number FROM Car WHERE rowid >= 1");
        
                 if(!carModal)  {carModal=new QSqlQueryModel(this); ui->trackSelect->setModel(carModal);}
                carModal->setQuery("SELECT name FROM Track WHERE rowid >= 1");
                
        
               if(!racetypeModal){
        racetypeModal=new QSqlQueryModel(this);
        ui->raceSelect->setModel(racetypeModal);
        }
                racetypeModal->setQuery("SELECT type FROM RaceType WHERE rowid >= 1");
            }
        

        Can I condense this any further

        Unless there are meaningful relations between the tables (and given the use case I doubt there are), no

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

        @VRonin Small hiccup I'm sure...

        if(!modal)  {modal=new QSqlQueryModel(this); ui->carSelect->setModel(modal);}
                modal->setQuery("SELECT type || ' ' || number FROM Car WHERE rowid >= 1");
        

        ^ Populates the dropdown, and updates the selections in the dropdown when run, but no longer displays the first item as it used to, and sets itself to blank selection after running the update.

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

          probably QComboBox not updating the current index and becoming invalid, add ui->carSelect->setCurrentIndex(0); after modal->setQuery

          "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

            probably QComboBox not updating the current index and becoming invalid, add ui->carSelect->setCurrentIndex(0); after modal->setQuery

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

            @VRonin That works wonderfully again. Much appreciated!

            1 Reply Last reply
            0

            • Login

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