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. error on operator= of QDateTime in a lambda, Qt5.6.2
Qt 6.11 is out! See what's new in the release blog

error on operator= of QDateTime in a lambda, Qt5.6.2

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 3 Posters 6.7k Views
  • 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.
  • S Offline
    S Offline
    stephane78
    wrote on last edited by kshegunov
    #1

    Hi, I have an error here

    QDateTime &QDateTime::operator=(const QDateTime &other)
    {
        d = other.d;//error
    

    inside a lambda function the code was:

    this->pf->setDateUpload2(du);
    

    but I have put du as member variable of my object and I have tried :

    this->pf->setDateUpload2(this->du);
    

    setDateUpload was

     void setDateUpload(const QDateTime &value);
    

    and I have tried with setDateUpload2 that is :

     void setDateUpload2(QDateTime value);
    

    du is a QdateTime
    this is a parameter of the lambda and is derived of QObject :

     connect(reponse,&QNetworkReply::finished,reponse,[this,reponse](){
    

    Has someone an idea to help me ?
    I use Qt5.6.2 and I am under windows,here with a debug version of Qt.....

    [Fixed code tags ~kshegunov]

    S m.sueM 2 Replies Last reply
    0
    • S stephane78

      Hi, I have an error here

      QDateTime &QDateTime::operator=(const QDateTime &other)
      {
          d = other.d;//error
      

      inside a lambda function the code was:

      this->pf->setDateUpload2(du);
      

      but I have put du as member variable of my object and I have tried :

      this->pf->setDateUpload2(this->du);
      

      setDateUpload was

       void setDateUpload(const QDateTime &value);
      

      and I have tried with setDateUpload2 that is :

       void setDateUpload2(QDateTime value);
      

      du is a QdateTime
      this is a parameter of the lambda and is derived of QObject :

       connect(reponse,&QNetworkReply::finished,reponse,[this,reponse](){
      

      Has someone an idea to help me ?
      I use Qt5.6.2 and I am under windows,here with a debug version of Qt.....

      [Fixed code tags ~kshegunov]

      S Offline
      S Offline
      stephane78
      wrote on last edited by stephane78
      #2

      there is an error on !old->ref.deref() (more inside, l93 of qshareddata.h)

      1 Reply Last reply
      0
      • S stephane78

        Hi, I have an error here

        QDateTime &QDateTime::operator=(const QDateTime &other)
        {
            d = other.d;//error
        

        inside a lambda function the code was:

        this->pf->setDateUpload2(du);
        

        but I have put du as member variable of my object and I have tried :

        this->pf->setDateUpload2(this->du);
        

        setDateUpload was

         void setDateUpload(const QDateTime &value);
        

        and I have tried with setDateUpload2 that is :

         void setDateUpload2(QDateTime value);
        

        du is a QdateTime
        this is a parameter of the lambda and is derived of QObject :

         connect(reponse,&QNetworkReply::finished,reponse,[this,reponse](){
        

        Has someone an idea to help me ?
        I use Qt5.6.2 and I am under windows,here with a debug version of Qt.....

        [Fixed code tags ~kshegunov]

        m.sueM Offline
        m.sueM Offline
        m.sue
        wrote on last edited by
        #3

        @stephane78

        Hi,

        You can try with a generic reference lambda [&]. Sometimes, depending on the compiler's implementation, lambdas require default objects to be created which may not work in all circumstances.

        -Michael.

        S 1 Reply Last reply
        1
        • m.sueM m.sue

          @stephane78

          Hi,

          You can try with a generic reference lambda [&]. Sometimes, depending on the compiler's implementation, lambdas require default objects to be created which may not work in all circumstances.

          -Michael.

          S Offline
          S Offline
          stephane78
          wrote on last edited by
          #4

          @m.sue what do you mean with generic lambda [&] ? when you pass "this" as argument of the lambda you don't pass it by reference but you can modify all the variable of "this" (with my compiler mingw-w64 and even with visual studio)

          m.sueM 1 Reply Last reply
          0
          • S stephane78

            @m.sue what do you mean with generic lambda [&] ? when you pass "this" as argument of the lambda you don't pass it by reference but you can modify all the variable of "this" (with my compiler mingw-w64 and even with visual studio)

            m.sueM Offline
            m.sueM Offline
            m.sue
            wrote on last edited by m.sue
            #5

            @stephane78

            I mean replace [this,reponse]() with [&]().

            -Michael.

            S 1 Reply Last reply
            1
            • m.sueM m.sue

              @stephane78

              I mean replace [this,reponse]() with [&]().

              -Michael.

              S Offline
              S Offline
              stephane78
              wrote on last edited by
              #6

              @m.sue no ,If I do that, I have an error on reponse->readAll() but I have tried [&,reponse] and I have always the same error on my operator = of QDateTime.

              J.HilkJ 1 Reply Last reply
              0
              • S stephane78

                @m.sue no ,If I do that, I have an error on reponse->readAll() but I have tried [&,reponse] and I have always the same error on my operator = of QDateTime.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @stephane78
                hi,
                I have a hard time understanding what exactly is the problem! Any chance you could post the whole lampda here?


                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
                • m.sueM Offline
                  m.sueM Offline
                  m.sue
                  wrote on last edited by
                  #8

                  ...and setDateUpload(2). I cannot find it as Qt function.

                  -Michael.

                  S 1 Reply Last reply
                  0
                  • m.sueM m.sue

                    ...and setDateUpload(2). I cannot find it as Qt function.

                    -Michael.

                    S Offline
                    S Offline
                    stephane78
                    wrote on last edited by
                    #9

                    @m.sue ```
                    void MyFichier::setDateUpload(const QDateTime &value)
                    {
                    dateUpload = value;
                    }
                    void MyFichier::setDateUpload2(QDateTime value)
                    {
                    dateUpload = value;
                    }

                    S 1 Reply Last reply
                    0
                    • S stephane78

                      @m.sue ```
                      void MyFichier::setDateUpload(const QDateTime &value)
                      {
                      dateUpload = value;
                      }
                      void MyFichier::setDateUpload2(QDateTime value)
                      {
                      dateUpload = value;
                      }

                      S Offline
                      S Offline
                      stephane78
                      wrote on last edited by stephane78
                      #10

                      @stephane78 these are my own functions...

                      S 1 Reply Last reply
                      0
                      • S stephane78

                        @stephane78 these are my own functions...

                        S Offline
                        S Offline
                        stephane78
                        wrote on last edited by stephane78
                        #11

                        here is the lambda

                        
                        connect(reponse,&QNetworkReply::finished,reponse,[this,reponse](){
                                
                               
                        
                              
                        
                        
                           QString strReply = QString::fromUtf8(reponse->readAll());
                            QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                            QJsonObject jsonObject = jsonResponse.object();
                        
                        
                            long idFichier = (long)jsonObject["fileId"].toDouble();
                           
                              QDateTime du = QDateTime::fromString(jsonObject["dateUpload"].toString(),"yyyy-MM-dd HH:mm:ss");
                        
                            QDateTime dup;
                            if(this->flag)
                                dup = QDateTime::fromString(jsonObject["updated_at"].toString(),"yyyy-MM-dd HH:mm:ss");
                           
                          
                          
                            int code=jsonObject["code"].toInt();
                        
                        
                        if(idFichier > 0)
                        {
                                 if(!this->flag)
                                    this->pf->setId(idFichier);
                                 if(this->flag)
                                     this->pf->setDateUpload2(dup);
                                 else this->pf->setDateUpload2(du);
                                 ....
                        
                        S 1 Reply Last reply
                        0
                        • S stephane78

                          here is the lambda

                          
                          connect(reponse,&QNetworkReply::finished,reponse,[this,reponse](){
                                  
                                 
                          
                                
                          
                          
                             QString strReply = QString::fromUtf8(reponse->readAll());
                              QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                              QJsonObject jsonObject = jsonResponse.object();
                          
                          
                              long idFichier = (long)jsonObject["fileId"].toDouble();
                             
                                QDateTime du = QDateTime::fromString(jsonObject["dateUpload"].toString(),"yyyy-MM-dd HH:mm:ss");
                          
                              QDateTime dup;
                              if(this->flag)
                                  dup = QDateTime::fromString(jsonObject["updated_at"].toString(),"yyyy-MM-dd HH:mm:ss");
                             
                            
                            
                              int code=jsonObject["code"].toInt();
                          
                          
                          if(idFichier > 0)
                          {
                                   if(!this->flag)
                                      this->pf->setId(idFichier);
                                   if(this->flag)
                                       this->pf->setDateUpload2(dup);
                                   else this->pf->setDateUpload2(du);
                                   ....
                          
                          S Offline
                          S Offline
                          stephane78
                          wrote on last edited by
                          #12

                          I think I could try with the release version because perhaps there isn't any error with the release version.....because here I am with the debug version and with the debugger and with a 64 bits debug version of Qt and Qtcreator....

                          S m.sueM 2 Replies Last reply
                          0
                          • S stephane78

                            I think I could try with the release version because perhaps there isn't any error with the release version.....because here I am with the debug version and with the debugger and with a 64 bits debug version of Qt and Qtcreator....

                            S Offline
                            S Offline
                            stephane78
                            wrote on last edited by
                            #13

                            OK this problem is solved, I have put a mutexlocker with a mutex before pf->setid and ml.unlock after setdateupload2 because this is an object associated to a secondary thread with movetothread and now I haven't any more this error. I Have another error further but I put a mutexlocker too and see

                            1 Reply Last reply
                            0
                            • S stephane78

                              I think I could try with the release version because perhaps there isn't any error with the release version.....because here I am with the debug version and with the debugger and with a 64 bits debug version of Qt and Qtcreator....

                              m.sueM Offline
                              m.sueM Offline
                              m.sue
                              wrote on last edited by m.sue
                              #14

                              @stephane78

                              There is something fishy with your lambda I think. You do not use &, which means the setDateUpload function works on a copy -> a temporary. You probably will want to work on something like:

                              QDateTime &dateUpload=pf->dateUpload;
                              connect(reponse,&QNetworkReply::finished,reponse,[&dateUpload,response](){
                              ...
                              });

                              EDIT: ok you did not tell about multithreading, either :-)

                              -Michael.

                              S 1 Reply Last reply
                              2
                              • m.sueM m.sue

                                @stephane78

                                There is something fishy with your lambda I think. You do not use &, which means the setDateUpload function works on a copy -> a temporary. You probably will want to work on something like:

                                QDateTime &dateUpload=pf->dateUpload;
                                connect(reponse,&QNetworkReply::finished,reponse,[&dateUpload,response](){
                                ...
                                });

                                EDIT: ok you did not tell about multithreading, either :-)

                                -Michael.

                                S Offline
                                S Offline
                                stephane78
                                wrote on last edited by
                                #15

                                @m.sue I have still the error on operator so I will now try what you said ,but I have already tried with du and dup a members variables of this and it was not good*....

                                S 1 Reply Last reply
                                0
                                • S stephane78

                                  @m.sue I have still the error on operator so I will now try what you said ,but I have already tried with du and dup a members variables of this and it was not good*....

                                  S Offline
                                  S Offline
                                  stephane78
                                  wrote on last edited by kshegunov
                                  #16

                                  @m-sue,Hi michael, thanks a lot,it was fast the solution : I have began to replace all the pointers on the instance of my own class with such a thing in the lambda and it seems to work : for example :

                                  MyFichier * &pf2=pf;
                                   connect(reponse,&QNetworkReply::finished,reponse,[this,&pf2,reponse](){
                                  

                                  and I haven't any problem any more with the QDateTime...

                                  S 1 Reply Last reply
                                  1
                                  • S stephane78

                                    @m-sue,Hi michael, thanks a lot,it was fast the solution : I have began to replace all the pointers on the instance of my own class with such a thing in the lambda and it seems to work : for example :

                                    MyFichier * &pf2=pf;
                                     connect(reponse,&QNetworkReply::finished,reponse,[this,&pf2,reponse](){
                                    

                                    and I haven't any problem any more with the QDateTime...

                                    S Offline
                                    S Offline
                                    stephane78
                                    wrote on last edited by
                                    #17

                                    I have still a problem with dateupload and shash...it seems that the other problems are solved (it seems ) but not for dateupload and shash....

                                    S 1 Reply Last reply
                                    0
                                    • S stephane78

                                      I have still a problem with dateupload and shash...it seems that the other problems are solved (it seems ) but not for dateupload and shash....

                                      S Offline
                                      S Offline
                                      stephane78
                                      wrote on last edited by
                                      #18

                                      still the error on !old->ref.deref()

                                      S 1 Reply Last reply
                                      0
                                      • S stephane78

                                        still the error on !old->ref.deref()

                                        S Offline
                                        S Offline
                                        stephane78
                                        wrote on last edited by stephane78
                                        #19

                                        OK, I am not sure that my compiler,mingw-w64 4.9.2, supports well the lambdas and I think I will try with mingw-w64 5.3.0 (but I must build a release and a debug version of Qt and my other libs with it......).

                                        S 1 Reply Last reply
                                        0
                                        • S stephane78

                                          OK, I am not sure that my compiler,mingw-w64 4.9.2, supports well the lambdas and I think I will try with mingw-w64 5.3.0 (but I must build a release and a debug version of Qt and my other libs with it......).

                                          S Offline
                                          S Offline
                                          stephane78
                                          wrote on last edited by
                                          #20

                                          so it not mingw 4.9.2. so is there any way to replace the loop.exec() in that code ?

                                          QNetworkReply * reponse = netmanager->post(requete,multiPart);
                                          
                                                
                                          
                                               QEventLoop loop;
                                                 
                                                 connect(reponse, SIGNAL(finished()), &loop, SLOT(quit())) ;
                                          
                                                 loop.exec();
                                          
                                          

                                          (but not a lambda because I have problems with the lambda)

                                          S 1 Reply Last reply
                                          0

                                          • Login

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