How to Populate QDateEdit Stored in QString again into a QDateEdit - qt

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"

Related

Create Qdate object from int or string

I want to make a specific date like 4-5-2016 (string or int) and convert it into a valid object that QDate can interact with.
Is there a way to convert or create a QDate from a string or an int in PySide?
Yes you can use QDate.fromString() or QDate.fromJulianDay()
Example:
d = QDate.fromString("4-5-2015", "d-M-yyyy")
See the QDate documentation

Convert TCHAR* to QString

How to convert easiest way in Qt?
int recordSize = 1000;
TCHAR* qRecord = new TCHAR[recordSize];
//here I get data form other function
//here I try to display
qString() << QString::fromWCharArray(qRecord,recordSize);//gives many ????
printf("%s",qRecord); // this work perfectly
I tried with wcstombs, formStdWString nad other but nothing seems to work.
Thanks for any help
QString s= (LPSTR)qRecord;
worked.
thanks
#kajojeq
no, your second answer is not correct. because when the encoding is set to utf16(or even utf8 sometimes) the s variable only save one character.
correct conversion is:
QString str = QString::fromWCharArray(qrecord)

QDateTime to QString with milliseconds in Qt3

Is there a way in Qt3 to convert QDateTime into a QString and back to QDateTime, so that eventually QDateTime will contain information about milliseconds?
Thanks.
Use the toString function. The format parameter determines the format of the result string.
For example the following code will return only the seconds and the miliseconds.
QDateTime t = QDateTime::currentDateTime ();
QString s = t.toString("ss:zzz");
PS. You should consider porting your code to Qt4
QString DataList[100][100] ;
QDateTime AwbDateTime ;
AwbDateTime = QDateTime::fromString(DataList[i][9].left(19),Qt::ISODate) ;
This will work fine in Qt3

Converting QString containing PostgreSQL timestamp to QDateTime

I'm having trouble with a seemingly very simple problem: I want to get a QDateTime from a QString containing a timestamp. I got the timestamp from PostgreSQL, but it doesn't matter. Here is the code that does not work:
QString timestamp = "2010-10-09 19:21:46+02:00";
QString format = "YYYY-MM-DD HH:MM:SSTZD";
QDateTime dt = QDateTime::fromString(timestamp, format);
qDebug() << dt.toString(); // outputs empty string
There must be something very obvious I'm missing. Thanks!
There were two mistakes I was making. There is no TZD in the format specifications, so I removed the time zone information since I do not need it in my app by doing:
timeStamp.chop(6);
And then used the following format to get a QDateTime. Note the lowercase format characters:
QDateTime createdAt = QDateTime::fromString(timeStamp, "yyyy-MM-dd HH:mm:ss");
Thanks to everyone above for helping out.

How to convert from timestamp to date in Qt?

I have a Unix timestamp and I need to convert it to human readable date + time. How can it be done in Qt?
int unixTime = 1234567890;
QDateTime timestamp;
timestamp.setTime_t(unixTime);
qDebug() << timestamp.toString(Qt::SystemLocaleShortDate);
That should get you going. Like Matthew said, see QDateTime.setTime_t, as well as QDateTime.toString. The toString has an enumeration with several different options, as well as an overload where you can pass a string allowing however much customization you like.
QDateTime.setTime_t
You can use the static function: "fromTime_t", like:
qDebug() << QDateTime::fromTime_t(your_time_stamp).toString("dd/MM/yyyy hh:mm:ss");
One good example
qint64 qiTimestamp=QDateTime::currentMSecsSinceEpoch();
QDateTime dt;
dt.setTime_t(qiTimestamp/1000);
ui->lineEdit_DateTime->setText(dt.toString("yyyy-MM-dd hh:mm:ss"));
Note that QDateTime::setTime_t() has been deprecated, use QDateTime::setSecsSinceEpoch(). Same for fromTime_t(): use QDateTime::fromSecsSinceEpoch().
As mentioned, instead of using the obsolete _t members here is a working example (Qt 5).
qint64 llTS=1591132400;
QString szFull=QDateTime::fromSecsSinceEpoch(llTS).toString("dddd d MMMM yyyy hh:mm:ss");
szFull: Tuesday 2 June 2020 23:13:20
Source: QDateTime (Qt 5)
Date formatting: QDate
Time formatting: QTime

Resources