Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. How to take input date from user in qdateedit and show database accordingly
Forum Updated to NodeBB v4.3 + New Features

How to take input date from user in qdateedit and show database accordingly

Scheduled Pinned Locked Moved India
22 Posts 4 Posters 15.7k 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.
  • V Offline
    V Offline
    vikchh00gmail.com
    wrote on 3 May 2014, 06:56 last edited by
    #1

    how to take input any date from user in qdateedit or calenderwidget and show database accordingly by clicking button now my code is showing databse of only current date(todays date) i want user to selct date and show data of that particular date

    @
    void MainWindow::on_pushButton_2_clicked()
    {
    QCalendarWidget *calendar=new QCalendarWidget;
    QDateEdit *dateedit=new QDateEdit;

    QDate d=calendar->selectedDate();
    ui->dateEdit->setDate(d);
    
    
    int  q= d.year();
    QString q1=QString::number(q);
    int q2=d.month();
    QString q3=QString::number(q2);
    int q4=d.day();
    QString q5=QString::number(q4);
    
    QSqlQueryModel *mode =new QSqlQueryModel(ui->print);
    mode->setQuery(QString("select * from health10  where month(Datestamp)=\""+q3+"\" and day(Datestamp)=\""+q5+"\" and year(datestamp)=\""+q1+"\" ;"));
    ui->print->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->print->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->print-> setModel (mode);
    

    @

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 May 2014, 20:31 last edited by
      #2

      Hi and welcome to devnet,

      If I follow your requirement correctly, you need to create a dialog for the user to pick a date. Once the user did, set the query on your model.

      You are currently recreating the model each time you click on pushButton2 which is a bad idea. You should set it up in the constructor and only update it's query there.

      Looks like you are a beginner, I would recommend that you first have a look at Qt's demos and example to get you starting.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shreesh
        wrote on 5 Dec 2014, 06:41 last edited by
        #3

        Hi. I have been trying to display date in the database into to datedit widget on UI form. But the date is not showing up. I am using sqlite database, and the datatype I have specified for it is "datetime". Below is the code I have written, where the date in database is pulled up successfully and display in "lineedit", but it doesnt show up if I use "dateedit" widget.

        @
        qry2->prepare("select date_of_birth from member where id="A1" or name="Ram"");
        QString d = qry2->value(0).toString();
        ui->lineEdit->setText(d);
        @

        Can you please tell me the code for displaying data in "dateedit" widget?

        Edit: Added code tags '@@' - p3c0

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shreesh
          wrote on 5 Dec 2014, 06:41 last edited by
          #4

          Hi. I have been trying to display date in the database into to datedit widget on UI form. But the date is not showing up. I am using sqlite database, and the datatype I have specified for it is "datetime". Below is the code I have written, where the date in database is pulled up successfully and display in "lineedit", but it doesnt show up if I use "dateedit" widget.

          @
          qry2->prepare("select date_of_birth from member where id="A1" or name="Ram"");
          QString d = qry2->value(0).toString();
          ui->lineEdit->setText(d);
          @

          Can you please tell me the code for displaying data in "dateedit" widget?

          Edit: Added code tags '@@' - p3c0

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shreesh
            wrote on 5 Dec 2014, 06:56 last edited by
            #5

            Also, I am trying to use checkboxes and radiobuttons. I can read the data from these and write some values into the database. But when I try to do the reverse, that is to read data back and accordingly check the radiobuttons I can do it.
            @
            if((string == "male"))
            {
            ui->radioButton_male->setChecked(1);
            }
            else
            ui->radioButton_female->setChecked(1);
            @

            But the same is not happening in case of check boxes.

            @
            if(string == "male")
            {
            ui->checkBox_yoga->setChecked(1);
            }
            @

            1 Reply Last reply
            0
            • S Offline
              S Offline
              shreesh
              wrote on 5 Dec 2014, 06:56 last edited by
              #6

              Also, I am trying to use checkboxes and radiobuttons. I can read the data from these and write some values into the database. But when I try to do the reverse, that is to read data back and accordingly check the radiobuttons I can do it.
              @
              if((string == "male"))
              {
              ui->radioButton_male->setChecked(1);
              }
              else
              ui->radioButton_female->setChecked(1);
              @

              But the same is not happening in case of check boxes.

              @
              if(string == "male")
              {
              ui->checkBox_yoga->setChecked(1);
              }
              @

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on 5 Dec 2014, 09:48 last edited by
                #7

                Hi,

                bq. Can you please tell me the code for displaying data in “dateedit” widget?

                QDateEdit expects QDate. So convert the output string to QDate and the set using "setDate":http://qt-project.org/doc/qt-5/qdatetimeedit.html#date-prop

                Please Rephrase the 2nd question. It's bit confusing to understand.

                157

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on 5 Dec 2014, 09:48 last edited by
                  #8

                  Hi,

                  bq. Can you please tell me the code for displaying data in “dateedit” widget?

                  QDateEdit expects QDate. So convert the output string to QDate and the set using "setDate":http://qt-project.org/doc/qt-5/qdatetimeedit.html#date-prop

                  Please Rephrase the 2nd question. It's bit confusing to understand.

                  157

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    shreesh
                    wrote on 6 Dec 2014, 04:13 last edited by
                    #9

                    Thank you for the reply on QDate.
                    Second question: My code accepts the value from radio-buttons and stores respective string values in database. And now I have another part of code, which reads the previously stored value and ticks the respective radio button. For this operation, the code I wrote is,

                    @
                    if((string == "male"))
                    {
                    ui->radioButton_male->setChecked(1);
                    }
                    else
                    ui->radioButton_female->setChecked(1);
                    @

                    Similar to radiobuttons, I want to use checkboxes. That is to read values from the database and check mark the respective checkboxes. The code for this I wrote is as below, but it is not checking though the query fetches the data.

                    @
                    if(string == "male")
                    {
                    ui->checkBox_male->setChecked(1);
                    }
                    else
                    {
                    ui->checkBox_female->setChecked(1);
                    }
                    @

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      shreesh
                      wrote on 6 Dec 2014, 04:13 last edited by
                      #10

                      Thank you for the reply on QDate.
                      Second question: My code accepts the value from radio-buttons and stores respective string values in database. And now I have another part of code, which reads the previously stored value and ticks the respective radio button. For this operation, the code I wrote is,

                      @
                      if((string == "male"))
                      {
                      ui->radioButton_male->setChecked(1);
                      }
                      else
                      ui->radioButton_female->setChecked(1);
                      @

                      Similar to radiobuttons, I want to use checkboxes. That is to read values from the database and check mark the respective checkboxes. The code for this I wrote is as below, but it is not checking though the query fetches the data.

                      @
                      if(string == "male")
                      {
                      ui->checkBox_male->setChecked(1);
                      }
                      else
                      {
                      ui->checkBox_female->setChecked(1);
                      }
                      @

                      1 Reply Last reply
                      0
                      • p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on 6 Dec 2014, 07:02 last edited by
                        #11

                        Please use code tags '@@' to post code.
                        Try using boolean values instead of integer. But that shouldn't matter actually. I guess there should be some other problem.

                        157

                        1 Reply Last reply
                        0
                        • p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on 6 Dec 2014, 07:02 last edited by
                          #12

                          Please use code tags '@@' to post code.
                          Try using boolean values instead of integer. But that shouldn't matter actually. I guess there should be some other problem.

                          157

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            shreesh
                            wrote on 6 Dec 2014, 08:13 last edited by
                            #13

                            Yes, I tried using boolean values too, problem still persists.
                            Thanks for the effort.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              shreesh
                              wrote on 6 Dec 2014, 08:13 last edited by
                              #14

                              Yes, I tried using boolean values too, problem still persists.
                              Thanks for the effort.

                              1 Reply Last reply
                              0
                              • p3c0P Offline
                                p3c0P Offline
                                p3c0
                                Moderators
                                wrote on 6 Dec 2014, 08:50 last edited by
                                #15

                                Can you post other parts of the code in relevance to this problem ?

                                157

                                1 Reply Last reply
                                0
                                • p3c0P Offline
                                  p3c0P Offline
                                  p3c0
                                  Moderators
                                  wrote on 6 Dec 2014, 08:50 last edited by
                                  #16

                                  Can you post other parts of the code in relevance to this problem ?

                                  157

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    shreesh
                                    wrote on 6 Dec 2014, 11:41 last edited by
                                    #17

                                    I am sorry for the partial code. Below is my actual code.

                                    @login_page conn;
                                    QString fill = ui->tableView->model()->data(index).toString();

                                    if(!conn.connopen())
                                    {
                                    qDebug()<<"Not connected";
                                    return;
                                    }

                                    conn.connopen();
                                    QSqlQuery * qry = new QSqlQuery();
                                    QSqlQuery * qry1 = new QSqlQuery();
                                    QSqlQuery * qry2 = new QSqlQuery();
                                    QSqlQuery * qry3 = new QSqlQuery();
                                    qry->prepare("select * from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                    qry1->prepare("select gender from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                    
                                    qry2->prepare("select dob from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                    qry3->prepare("select yoga,martial_arts,aerobics,zumba,general,swimming,cardio,studio_cycel from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                    
                                    
                                    if(qry->exec&#40;&#41; and qry1->exec(&#41; and qry2->exec(&#41; and qry3->exec(&#41;&#41;
                                    {
                                        while(qry->next(&#41; and qry1->next(&#41; and qry2->next(&#41; and qry3->exec(&#41;&#41;
                                        {
                                          QString str = qry1->value(0&#41;.toString(&#41;;
                                          //QString y,ma,a,z,g,s,sc,c;
                                          QString y = qry3->value(0&#41;.toString(&#41;;
                                          QString ma = qry3->value(1&#41;.toString(&#41;;
                                          QString a = qry3->value(2&#41;.toString(&#41;;
                                          QString z = qry3->value(3).toString();
                                          QString g = qry3->value(4).toString();
                                          QString s = qry3->value(5).toString();
                                          QString c = qry3->value(6).toString();
                                          QString sc = qry3->value(7).toString();
                                    
                                          ui->txt_iid->setText(qry->value(0).toString());
                                          ui->txt_iname->setText(qry->value(1).toString());
                                          if((str == "male"))
                                          {
                                          ui->radioButton_male_2->setChecked(1);
                                          }
                                          else
                                          ui->radioButton_female_2->setChecked(1);
                                    
                                         
                                    
                                          if(y == "Yoga")
                                          {
                                              ui->checkBox_yoga->setChecked(1);
                                          }
                                          if(ma == "Martial arts")
                                          {
                                              ui->checkBox_ma->setChecked(1);
                                          }
                                          if((a == "Aerobics"))
                                          {
                                              ui->checkBox_a->setChecked(1);
                                          }
                                          if((z == "Zumba"))
                                          {
                                              ui->checkBox_z->setChecked(1);
                                          }
                                          if((g == "General"))
                                          {
                                              ui->checkBox_g->setChecked(1);
                                          }
                                          if((s == "Swimming"))
                                          {
                                              ui->checkBox_s->setChecked(1);
                                          }
                                          if((c == "Cardio"))
                                          {
                                              ui->checkBox_c->setChecked(1);
                                          }
                                          if((sc == "Studio cycle"))
                                          {
                                              ui->checkBox_sc->setChecked(1);
                                          }
                                    
                                          ui->txt_phone->setText(qry->value(12).toString());
                                          ui->txt_street->setText(qry->value(13).toString());
                                          ui->txt_apt->setText(qry->value(14).toString());
                                          ui->txt_city->setText(qry->value(15).toString());
                                          ui->txt_state->setText(qry->value(16).toString());
                                          ui->txt_zip->setText(qry->value(17).toString());
                                    
                                          conn.connclose();
                                        }
                                    
                                    }
                                    else
                                        QMessageBox::information(this,tr("Error"),tr("Error in diaplaying")); @
                                    
                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      shreesh
                                      wrote on 6 Dec 2014, 11:41 last edited by
                                      #18

                                      I am sorry for the partial code. Below is my actual code.

                                      @login_page conn;
                                      QString fill = ui->tableView->model()->data(index).toString();

                                      if(!conn.connopen())
                                      {
                                      qDebug()<<"Not connected";
                                      return;
                                      }

                                      conn.connopen();
                                      QSqlQuery * qry = new QSqlQuery();
                                      QSqlQuery * qry1 = new QSqlQuery();
                                      QSqlQuery * qry2 = new QSqlQuery();
                                      QSqlQuery * qry3 = new QSqlQuery();
                                      qry->prepare("select * from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                      qry1->prepare("select gender from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                      
                                      qry2->prepare("select dob from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                      qry3->prepare("select yoga,martial_arts,aerobics,zumba,general,swimming,cardio,studio_cycel from instructor where i_id='"+fill+"' or i_name='"+fill+"'");
                                      
                                      
                                      if(qry->exec&#40;&#41; and qry1->exec(&#41; and qry2->exec(&#41; and qry3->exec(&#41;&#41;
                                      {
                                          while(qry->next(&#41; and qry1->next(&#41; and qry2->next(&#41; and qry3->exec(&#41;&#41;
                                          {
                                            QString str = qry1->value(0&#41;.toString(&#41;;
                                            //QString y,ma,a,z,g,s,sc,c;
                                            QString y = qry3->value(0&#41;.toString(&#41;;
                                            QString ma = qry3->value(1&#41;.toString(&#41;;
                                            QString a = qry3->value(2&#41;.toString(&#41;;
                                            QString z = qry3->value(3).toString();
                                            QString g = qry3->value(4).toString();
                                            QString s = qry3->value(5).toString();
                                            QString c = qry3->value(6).toString();
                                            QString sc = qry3->value(7).toString();
                                      
                                            ui->txt_iid->setText(qry->value(0).toString());
                                            ui->txt_iname->setText(qry->value(1).toString());
                                            if((str == "male"))
                                            {
                                            ui->radioButton_male_2->setChecked(1);
                                            }
                                            else
                                            ui->radioButton_female_2->setChecked(1);
                                      
                                           
                                      
                                            if(y == "Yoga")
                                            {
                                                ui->checkBox_yoga->setChecked(1);
                                            }
                                            if(ma == "Martial arts")
                                            {
                                                ui->checkBox_ma->setChecked(1);
                                            }
                                            if((a == "Aerobics"))
                                            {
                                                ui->checkBox_a->setChecked(1);
                                            }
                                            if((z == "Zumba"))
                                            {
                                                ui->checkBox_z->setChecked(1);
                                            }
                                            if((g == "General"))
                                            {
                                                ui->checkBox_g->setChecked(1);
                                            }
                                            if((s == "Swimming"))
                                            {
                                                ui->checkBox_s->setChecked(1);
                                            }
                                            if((c == "Cardio"))
                                            {
                                                ui->checkBox_c->setChecked(1);
                                            }
                                            if((sc == "Studio cycle"))
                                            {
                                                ui->checkBox_sc->setChecked(1);
                                            }
                                      
                                            ui->txt_phone->setText(qry->value(12).toString());
                                            ui->txt_street->setText(qry->value(13).toString());
                                            ui->txt_apt->setText(qry->value(14).toString());
                                            ui->txt_city->setText(qry->value(15).toString());
                                            ui->txt_state->setText(qry->value(16).toString());
                                            ui->txt_zip->setText(qry->value(17).toString());
                                      
                                            conn.connclose();
                                          }
                                      
                                      }
                                      else
                                          QMessageBox::information(this,tr("Error"),tr("Error in diaplaying")); @
                                      
                                      1 Reply Last reply
                                      0
                                      • p3c0P Offline
                                        p3c0P Offline
                                        p3c0
                                        Moderators
                                        wrote on 6 Dec 2014, 14:18 last edited by
                                        #19

                                        Sorry but the code looks little bit weird. I think you must first rectify them:

                                        1. No need to open the database twice.
                                        2. Since you are fetching everything from the same table with same where clauses why unnecessarily complicate the code by firing 4 different queries.
                                        3. Furthermore it complicates the if and while conditions.
                                        4. Closing the connection in while loop would be a bad idea as it will close the database and the end of first iteration itself and thus affecting the remaining data if any.

                                        157

                                        1 Reply Last reply
                                        0
                                        • p3c0P Offline
                                          p3c0P Offline
                                          p3c0
                                          Moderators
                                          wrote on 6 Dec 2014, 14:18 last edited by
                                          #20

                                          Sorry but the code looks little bit weird. I think you must first rectify them:

                                          1. No need to open the database twice.
                                          2. Since you are fetching everything from the same table with same where clauses why unnecessarily complicate the code by firing 4 different queries.
                                          3. Furthermore it complicates the if and while conditions.
                                          4. Closing the connection in while loop would be a bad idea as it will close the database and the end of first iteration itself and thus affecting the remaining data if any.

                                          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