Solved copy from QStringList to QString
-
@K-Str said in copy from QStringList to QString:
is it possible that the QString item QTS in uninitialized.
could you please show a more complete code snippet of what you have so far?
-
@Pablo-J-Rogina said in copy from QStringList to QString:
could you please show a more complete code snippet of what you have so far?
Hi Pablo J. Rogina,
thanks for your answer,
I think there is something wrong by using memset .void ClassSqlite::conv_tm_to_time_t() { char buffer1[80]; QString qs; time_t rawtime_akt, rawtime_neu; struct tm * timeinfo_akt, timeinfo_neu; qDebug() << CSVliste.size(); qDebug() << qs.size(); qs = CSVliste[0]; memset (&timeinfo_akt, 0, sizeof(struct tm)); <<<==== this statement overwites qs time (&rawtime_akt); timeinfo_akt = localtime (&rawtime_akt); strftime (buffer1,80,"%H:%M:%S",timeinfo_akt); Uhrzeit = buffer1; strftime (buffer1,80,"%e.%m.%Y", timeinfo_akt); Datum = buffer1; memset (&timeinfo_neu, 0, sizeof(struct tm)); strptime(qs.toStdString().c_str(), "%d-%m-%Y %H:%M:%S", &timeinfo_neu); // rawtime_neu=mktime(&timeinfo_neu); // qDebug() << "Der unteschied ist: " << (float)rawtime_akt-(float)rawtime_neu; }
Is there a way around tihs?
-
@K-Str said in copy from QStringList to QString:
struct tm * timeinfo_akt, timeinfo_neu; memset (&timeinfo_akt, 0, sizeof(struct tm)); <<<==== this statement overwites qs
timeinfo_akt
is just a pointer-size. This overwrites the stack. You meantstruct tm timeinfo_akt, timeinfo_neu;
You should not be using any of this code in Qt/nowadays. Seriously.
-
Hi,
Would it not be simpler to use QDateTime ?
-
Hi SGaist,
thanks for your answer.
I tried to use QDAteTime in the following way:#include <QCoreApplication> #include <QDebug> #include <QString> #include <QDateTime> #include <string> using namespace std; bool TimeCheck() { QString QS_OldTime = "2020-08-20 09:48:33"; QString QS_Format = "yyyy-MM-dd hh:mm.ss"; QDateTime QD_OldTime, QD_AktTime; QD_AktTime = QDateTime::currentDateTime(); QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format); return true; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); if (TimeCheck()) { qDebug() << "OK"; } else { qDebug() << "NOK"; } return a.exec(); }
This little code should be used to check the difference betweem the aktual time and the old time
But i'm quiete confused because I do not get the expected results:-
The values the values for
actual date and time : QDateTime(2020-08-20 12:19:19.182 CEST Qt::TimeSpec(LocalTime))
Old date and time : QDateTime( Qt::TimeSpec(LocalTime)) -
How can I get values where I can check easily how the difference is in seconds.
Whats wrong with the statement
QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);
-
-
@K-Str
do you spot the difference in format to actual text?09:48
:
33
hh:mm.
ss -
hi J.Hilk.
thanks for your answer!
yes but it would be better to have it in seconds like 900 seconds for 15 minutes. -
@K-Str what do you want in seconds, from what starting point/time
-
Hi j.Hilk,
If it is possibel to get something like from time_t mode. -
-
J.Hilk,
thanks a lot for this tip.
It works perfect.
But how can I get the milliseconds since a given time likeQString QS_OldTime = "2020-08-20 09:48:33";
This staement
QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);
doesn't work correctly
-
This statement
QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);
doesn't work correctlyit doesn't work correctly because your QS_Format is wrong, like I posted previously
But how can I get the milliseconds since a given
-
Hi j.Hilk,
ok the partQT_Akt=QDateTime::currentMSecsSinceEpoch();
works perfect!
But what about this part?QString QS_OldTime = "2020-08-15 09:48:33"; QString QS_Format = "yyyy-MM-dd hh:mm.ss"; QDateTime QD_OldTime, QD_AktTime; QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);
I saw the value of QD_OldTime is not correctly assigned
There was a "." inQString QS_Format = "yyyy-MM-dd hh:mm.ss";
on the place ":mm.ss"
Now it works perfect.
Thanks a lot! -
@K-Str
Just to say: I was the peson who said you should not be using your old C code withmemcpy()
etc. Your new code withQString
,QDateTime
looks a lot neater and more C++, well done! :) -
@K-Str said in copy from QStringList to QString:
But what about this part?
QString QS_OldTime = "2020-08-15 09:48:33";
QString QS_Format = "yyyy-MM-dd hh:mm.ss";
QDateTime QD_OldTime, QD_AktTime;
QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);This can NOT work because the pattern are not matching.
You have to changeQS_OldTime
orQS_Format
.
Read carefully and you will see your typo ;)