Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help on QDate , Tableview (logic error)
QtWS25 Last Chance

Help on QDate , Tableview (logic error)

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.0k Views
  • 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 Offline
    S Offline
    shreesh
    wrote on last edited by
    #1

    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
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        shreesh
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

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

          157

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved