Help on QDate , Tableview (logic error)



  • I an application I am using the extracted month from Qdate and then to match it with “month” specified in a combobox. There are multiple database entries for a month, but in the output “tableview” only the last matching row is displaying. Below is the code I wrote.
    Can somebody tell me if there is any mistake in the logic?

    @QString filter_month = ui->comboBox_month->currentText();
    QString filter_year = ui->comboBox_year->currentText();

    conn.connopen();
    QSqlQuery * qry = new QSqlQuery();
    
    qry->prepare("select class_date from schedule_class");
    
    if(qry->exec())
    {
    
    
        while(qry->next())
        {
            QString expt = qry->value(0).toString();
    
            QSqlQueryModel * modal = new QSqlQueryModel();
    
            QDate extract(QDate::fromString(expt, "MM/dd/yyyy"));
    
            int m = extract.month();
            QString month = QString::number(m);
    
            int y = extract.year();
            QString year = QString::number(y);
    
          if(month == filter_month and year == filter_year)
          {
          QSqlQuery * qry2 = new QSqlQuery();
          qry2->prepare("select * from schedule_class where class_date='"+expt+"'");
          qry2->exec();
    
          modal->setQuery(* qry2);
    
          }
        ui->tableView_classes->setModel(modal);
        }
        conn.connclose();
    }@

  • Moderators

    Hi,

    @
    ui->tableView_classes->setModel(modal);
    @

    You are doing this in the while loop so naturally the last entry in the query result would be reflected in the table.

    Also there are few memory leaks in the code.



  • Yes, I recognise it now. Thank you, it worked!


  • Moderators

    Glad to hear it worked :) Please mark the post as solved by editing the title and prepending [solved]


Log in to reply
 

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