简体   繁体   中英

How to Populate QDateEdit Stored in QString again into a QDateEdit

I have 2 QDateEdit which are Date_dob and Date_doj. I am storing the value using a Qstring shown below.

QString str_dob(ui->DATE_dob->text());
QString str_doj(ui->DATE_doj->text());

Now i want to populate the same into ui->Date_dob and ui->Date_doj (after some editing event takes place). I have used ,

ui->DATE_dob->setText(s.at(2));
ui->DATE_doj->setText(s.at(5)); //where s is a string having data

but the data doesn't populate.

Any kind of suggestion will be extremely appreciated. Thanks in advance

For convert QString to QDate you can use QDate::fromString() . Then you can set date in QDateEdit with QDate::setDate(const QDate &date) .

Hope it help.

You use wrong way the conversion.

QDate to QString

QString str_dob = ui->DATE_dob->toString("dd MM yyyy");

in the date format you should specify it else your conversation is default format. Known Format you can use

QString to QDate

if( ui->DATE_dob->setDate(QDate::fromString(str_dob,"dd MM yyyy").year()\
,QDate::fromString(str_dob,"dd MM yyyy").month()\
,QDate::fromString(str_dob,"dd MM yyyy").day()){
// Your Conversation Succes
}

when QString to QDate you have to know date format in string else your conversation fail or wrong value you get.

Example: if Qstring is : 19/12/2017 than your format is "dd/MM/yyyy"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM