How to take input date from user in qdateedit and show database accordingly
-
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]
-
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
-
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
-
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
-
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);
}
@ -
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);
}
@ -
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.
-
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.
-
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);
}
@ -
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);
}
@ -
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. -
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. -
Can you post other parts of the code in relevance to this problem ?
-
Can you post other parts of the code in relevance to this problem ?
-
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() and qry1->exec() and qry2->exec() and qry3->exec()) { while(qry->next() and qry1->next() and qry2->next() and qry3->exec()) { QString str = qry1->value(0).toString(); //QString y,ma,a,z,g,s,sc,c; QString y = qry3->value(0).toString(); QString ma = qry3->value(1).toString(); QString a = qry3->value(2).toString(); 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")); @
-
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() and qry1->exec() and qry2->exec() and qry3->exec()) { while(qry->next() and qry1->next() and qry2->next() and qry3->exec()) { QString str = qry1->value(0).toString(); //QString y,ma,a,z,g,s,sc,c; QString y = qry3->value(0).toString(); QString ma = qry3->value(1).toString(); QString a = qry3->value(2).toString(); 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")); @
-
Sorry but the code looks little bit weird. I think you must first rectify them:
- No need to open the database twice.
- Since you are fetching everything from the same table with same where clauses why unnecessarily complicate the code by firing 4 different queries.
- Furthermore it complicates the if and while conditions.
- 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.
-
Sorry but the code looks little bit weird. I think you must first rectify them:
- No need to open the database twice.
- Since you are fetching everything from the same table with same where clauses why unnecessarily complicate the code by firing 4 different queries.
- Furthermore it complicates the if and while conditions.
- 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.