Lalaboy
Lalaboy

Reputation: 29

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

Upvotes: 0

Views: 513

Answers (2)

CMLDMR
CMLDMR

Reputation: 344

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"

Upvotes: 0

Konstantin T.
Konstantin T.

Reputation: 1013

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.

Upvotes: 0

Related Questions