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. [SOLVED] QObject inheritance problems with operator=() method
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.8k 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.
  • D Offline
    D Offline
    Divitto
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Divitto
        wrote on last edited by
        #3

        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 :)

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Divitto
            wrote on last edited by
            #5

            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.

            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