Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. copy from QStringList to QString
Forum Updated to NodeBB v4.3 + New Features

copy from QStringList to QString

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 7 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K-Str
    wrote on 18 Aug 2020, 15:49 last edited by K-Str
    #1

    Hi,
    since some days I try to copy one element of a QStrinList to a QList value.

    QTS = QTSL[0];
    

    Where

    • QTS is a simple QTString
    • list itemQTSL[0] is an element of QTStringList

    But in this way it doesn't work.
    I get an error:
    The inferior stopped because it received a signal from the operating system.

    Signal name :
    SIGSEGV
    Signal meaning :
    Segmentation fault

    I think a solution is quiete simple.
    Please could someone help me?
    Thanks!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      khryleption
      wrote on 18 Aug 2020, 15:54 last edited by
      #2

      Hi,
      Maybe your list is empty, do you check the list size ?

      1 Reply Last reply
      1
      • K Offline
        K Offline
        K-Str
        wrote on 18 Aug 2020, 15:59 last edited by
        #3

        Hi khryleption ,
        thanks for your answer.
        The list is not empty and in the elemnt QTSL[0] are also some data.
        qDebug << QTSL[0] shows the expected content.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          K-Str
          wrote on 18 Aug 2020, 16:33 last edited by
          #4

          Hi,
          is it possible that the QString item QTS in uninitialized.
          If yes how can I solve this?

          P 1 Reply Last reply 18 Aug 2020, 16:36
          0
          • K K-Str
            18 Aug 2020, 16:33

            Hi,
            is it possible that the QString item QTS in uninitialized.
            If yes how can I solve this?

            P Offline
            P Offline
            Pablo J. Rogina
            wrote on 18 Aug 2020, 16:36 last edited by
            #5

            @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?

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • K Offline
              K Offline
              K-Str
              wrote on 18 Aug 2020, 17:00 last edited by K-Str
              #6

              @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?

              J 1 Reply Last reply 18 Aug 2020, 17:15
              0
              • K K-Str
                18 Aug 2020, 17:00

                @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?

                J Online
                J Online
                JonB
                wrote on 18 Aug 2020, 17:15 last edited by JonB
                #7

                @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 meant

                struct tm timeinfo_akt, timeinfo_neu;
                

                You should not be using any of this code in Qt/nowadays. Seriously.

                1 Reply Last reply
                4
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 18 Aug 2020, 18:02 last edited by
                  #8

                  Hi,

                  Would it not be simpler to use QDateTime ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • K Offline
                    K Offline
                    K-Str
                    wrote on 20 Aug 2020, 10:29 last edited by K-Str
                    #9

                    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);
                    
                    J.HilkJ 1 Reply Last reply 20 Aug 2020, 10:41
                    0
                    • K K-Str
                      20 Aug 2020, 10:29

                      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);
                      
                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on 20 Aug 2020, 10:41 last edited by
                      #10

                      @K-Str
                      do you spot the difference in format to actual text?

                      09:48:33
                      hh:mm.ss


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      3
                      • K Offline
                        K Offline
                        K-Str
                        wrote on 20 Aug 2020, 10:44 last edited by K-Str
                        #11

                        hi J.Hilk.
                        thanks for your answer!
                        yes but it would be better to have it in seconds like 900 seconds for 15 minutes.

                        J.HilkJ 1 Reply Last reply 20 Aug 2020, 11:02
                        0
                        • K K-Str
                          20 Aug 2020, 10:44

                          hi J.Hilk.
                          thanks for your answer!
                          yes but it would be better to have it in seconds like 900 seconds for 15 minutes.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on 20 Aug 2020, 11:02 last edited by
                          #12

                          @K-Str what do you want in seconds, from what starting point/time


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            K-Str
                            wrote on 20 Aug 2020, 11:23 last edited by
                            #13

                            Hi j.Hilk,
                            If it is possibel to get something like from time_t mode.

                            J.HilkJ 1 Reply Last reply 20 Aug 2020, 11:34
                            0
                            • K K-Str
                              20 Aug 2020, 11:23

                              Hi j.Hilk,
                              If it is possibel to get something like from time_t mode.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on 20 Aug 2020, 11:34 last edited by
                              #14

                              @K-Str
                              https://doc.qt.io/qt-5/qdatetime.html#toSecsSinceEpoch


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              1
                              • K Offline
                                K Offline
                                K-Str
                                wrote on 20 Aug 2020, 12:00 last edited by K-Str
                                #15

                                J.Hilk,
                                thanks a lot for this tip.
                                It works perfect.
                                But how can I get the milliseconds since a given time like

                                    QString QS_OldTime = "2020-08-20 09:48:33";
                                

                                This staement

                                    QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);
                                

                                doesn't work correctly

                                J.HilkJ 1 Reply Last reply 20 Aug 2020, 12:09
                                0
                                • K K-Str
                                  20 Aug 2020, 12:00

                                  J.Hilk,
                                  thanks a lot for this tip.
                                  It works perfect.
                                  But how can I get the milliseconds since a given time like

                                      QString QS_OldTime = "2020-08-20 09:48:33";
                                  

                                  This staement

                                      QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);
                                  

                                  doesn't work correctly

                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on 20 Aug 2020, 12:09 last edited by
                                  #16

                                  @K-Str

                                  This statement
                                  QD_OldTime = QDateTime::fromString(QS_OldTime, QS_Format);
                                  doesn't work correctly

                                  it doesn't work correctly because your QS_Format is wrong, like I posted previously

                                  But how can I get the milliseconds since a given

                                  https://doc.qt.io/qt-5/qdatetime.html#msecsTo


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    K-Str
                                    wrote on 20 Aug 2020, 12:34 last edited by K-Str
                                    #17

                                    Hi j.Hilk,
                                    ok the part

                                        QT_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 "." in

                                    QString QS_Format = "yyyy-MM-dd hh:mm.ss";
                                    

                                    on the place ":mm.ss"
                                    Now it works perfect.
                                    Thanks a lot!

                                    J KroMignonK 2 Replies Last reply 20 Aug 2020, 13:08
                                    0
                                    • K K-Str
                                      20 Aug 2020, 12:34

                                      Hi j.Hilk,
                                      ok the part

                                          QT_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 "." in

                                      QString QS_Format = "yyyy-MM-dd hh:mm.ss";
                                      

                                      on the place ":mm.ss"
                                      Now it works perfect.
                                      Thanks a lot!

                                      J Online
                                      J Online
                                      JonB
                                      wrote on 20 Aug 2020, 13:08 last edited by
                                      #18

                                      @K-Str
                                      Just to say: I was the peson who said you should not be using your old C code with memcpy() etc. Your new code with QString, QDateTime looks a lot neater and more C++, well done! :)

                                      1 Reply Last reply
                                      1
                                      • K K-Str
                                        20 Aug 2020, 12:34

                                        Hi j.Hilk,
                                        ok the part

                                            QT_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 "." in

                                        QString QS_Format = "yyyy-MM-dd hh:mm.ss";
                                        

                                        on the place ":mm.ss"
                                        Now it works perfect.
                                        Thanks a lot!

                                        KroMignonK Offline
                                        KroMignonK Offline
                                        KroMignon
                                        wrote on 20 Aug 2020, 13:15 last edited by
                                        #19

                                        @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 change QS_OldTime or QS_Format.
                                        Read carefully and you will see your typo ;)

                                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                        1 Reply Last reply
                                        1

                                        1/19

                                        18 Aug 2020, 15:49

                                        • Login

                                        • Login or register to search.
                                        1 out of 19
                                        • First post
                                          1/19
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved