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. QDateTime copy constructor weird behaviour
Forum Updated to NodeBB v4.3 + New Features

QDateTime copy constructor weird behaviour

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.7k Views 1 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.
  • F Offline
    F Offline
    fscibilia
    wrote on last edited by
    #1

    Hi everyone,

    I have a QList<Item> where Item is a class containing a field timestamp of type QDateTime. When I push_back a new item in the list using code like this:

    class Item {
    QDateTime timestamp;
    ....
    }

    Item item;
    list.push_back(item);

    I expect that the push_back method appends a copy of the item (made using copy constructor of Item) to the list. By default the copy constructor of a class invokes copy constructor of each field declared within that class. In this case copy constructor of Item will invoke copy constructor of QDateTime to create a copy of field timestamp.

    What strangely happens is that, after the insertion, when the Qt Gui terminates its normal thread loop, the program crashes. If I remove the field timestamp from class Item, everything works fine.

    I wonder why, given that QDateTime is not derived from QObject and there is not special attention to pay for its normal construction/destruction.

    FYI: I've been using Qt for not more than 2 months and may be I miss something.

    Thanks a lot for your help

    Fabio

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Can you provide a minimum sample of code that shows this behavior ?

      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
      0
      • F Offline
        F Offline
        fscibilia
        wrote on last edited by
        #3

        Hi SGalst,

        pasting the code is a bit complex at the moment due to the depth of the invocations. One more info I van give right now is that in debug mode that problem does not raise and the application works correctly! :-(

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi,
          Your conclusion that the QList will copy your class is incorrect. It will seem like that on the outside, but QList will only push to pointer to the next position. A QList is buid up out of pointers, not real classes. Only when elements in the list are smaller in data containment then a pointer will it use the real elements. So in your case, the pointer will get shifted and no copy will be made.
          Btw the push_Back function is for STL compatibility, it's better to use the append function in QList.
          This said, it is probably not the cause of your crash. What do you mean with
          bq. when the Qt Gui terminates its normal thread loop

          The QtGui thread is run in the QApplication, so when that stops, all events are halted. There may only be one thread with Gui elements active and that is ALWAYS the main thread. So when you have a GUI application, you may not and expect no event handling or proper code execution when the QApplication is closed.
          So, like SGaist says, please some code. Maybe the class definition??

          Greetz, Jeroen

          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