Important: Please read the Qt Code of Conduct -

Issue with QList and Copy QList with objects made of QObject

  • Hi,
    I have list of objects(made of QObject - myList<myObject>).
    For some reasons user may be want to modify this list or maybe he/she want to cancel the modifications.
    The solution which has came into my mind is to make a copy of my list and give it to user while keeping original one.
    so whenever user modify copied list, i have original list so i can change it or revert it based on what user wants.
    But the problem is that the objects are made of QObject, so they can not be copied(Copy constructor and
    assignment have disabled in qobject)!!! I don't know what else to do?!
    If i define myObject as pointers then i can't copy the list because they have defined as pointer
    Thanks if someone can help me through this. or may be i can use another approach?

  • Hi,
    Do you want to modify list of your objects? or your objects in your list?
    If the first then I think the copy is not needed at all, you can store pointers and move them somewhere if necessary.
    If the second then consider "'memento' design pattern":, it helps to store history of object's states.

    Best regards

  • Thanks for quick reply,
    Both, user can add or remove from list and can edit objects in the list.

  • There is also another solution: create 'clone' method which produces exact copy of your object.

  • Thanks, got it :)

  • Lifetime Qt Champion


    One question: Does your class really need to be derived from QObject ?

Log in to reply