Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Condensing sequential setQuery?

    General and Desktop
    2
    5
    757
    Loading More Posts
    • 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
      andrewhopps last edited by

      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 Reply Quote 0
      • V
        VRonin last edited by 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

        "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 Reply Quote 3
        • A
          andrewhopps @VRonin last edited by

          @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 Reply Quote 0
          • V
            VRonin last edited by

            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 Reply Quote 2
            • A
              andrewhopps @VRonin last edited by

              @VRonin That works wonderfully again. Much appreciated!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post