Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Help on QDate , Tableview (logic error)

    General and Desktop
    2
    4
    869
    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.
    • S
      shreesh last edited by

      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();
      }@
      
      1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        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.

        157

        1 Reply Last reply Reply Quote 0
        • S
          shreesh last edited by

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

          1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators last edited by

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

            157

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