Important: Please read the Qt Code of Conduct -

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();

    QSqlQuery * qry = new QSqlQuery();
    qry->prepare("select class_date from schedule_class");
            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+"'");
          modal->setQuery(* qry2);

  • Moderators



    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