[SOLVED] QObject inheritance problems with operator=() method



  • I know ( after some research ) that you can't directly use the = operator on classes that inherit from QObect because the operator= method is declared private by people way smarter then me in the QObject code. This may sound dumb, but is it possible to use the = operator on an object who has a ( composition ) object that inherits from QObject? Something like:
    @
    class foo : public QObject
    {
    Q_OBJECT
    // some stuff...
    };

    class bar
    {
    private:
    // Attributes
    Foo foo;

     // some stuff...
    

    };

    int main()
    {
    Foo foo1 = Foo(); // not sure if this is legal
    Foo foo2 = foo1; // Doesn't work

      Bar bar1 = Bar();   // are either of these legal in Qt?
      Bar bar2 = bar1; 
    

    }

    @

    what if all my variables in main were pointers would they work then?

    @
    int main( )
    {
    Foo* foo1 = new Foo();
    Foo* foo2 = foo1;

     Bar* bar1 =  new Bar();
     Bar* bar2 = bar1;
    

    }
    @

    The reason i ask is i have many cases of composition in my current project, the smallest component of which, needs to be a QObject so that i can use the Signal/slot features of Qt. I apoligize if this is confusing, but i myself am confused. Please ask for clarification where/if its needed. THANKS!



  • In short word, you can not copy a QObject or Class inherits from QObject.

    Please the manual carefully http://qt-project.org/doc/qt-4.8/object.html

    BTW, I don't think that, copying the QObject is needed for you, you can always use the pointer of QObject and use the smart Pointers provided by C++ or Qt.



  • I'm always ready to hear another solution to a problem, what do you mean use the pointer of QObject? could you clarify or send me a link? I am also not familiar with smart pointers, i'm pretty sure most of my pointers are dumb ones :p sorry i am new to the Qt way of things i havent made it through all the documentation yet, although it has helped quite a bit :)


  • Moderators

    in (very) short words:
    use the new keyword to create objects and assign the pointers instead of the object values.

    Examples for smart pointers in Qt see QSharedPointer and QScopedPointer classes; for weak pointer use QPointer class



  • Thanks for your responses. All my objects were pointers which is why i was confused as to why they wern't working. The problem ended up being that a getter method was returning a dereferenced pointer (don't ask me why) and my code was using that method throughout, which (obviously) explains why i couldn't copy it. I'm sorry this question was kind of dumb. In my defence i had been staring at my code for at least 5 hours trying to figure out what was wrong so i probably shuld have taken a break, and came back before asking for help. :p

    Thank you for your patience.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.