Condensing sequential setQuery?



  • 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)



  • 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



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



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



  • @VRonin That works wonderfully again. Much appreciated!


Log in to reply
 

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