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)
Forum Update on Tuesday, May 27th 2025

Help on QDate , Tableview (logic error)

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.0k Views 1 Watching
  • 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 7 Dec 2014, 01:43 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
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 7 Dec 2014, 04:51 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 7 Dec 2014, 21:54 last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 8 Dec 2014, 05:14 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

          1/4

          7 Dec 2014, 01:43

          • Login

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