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. Class Promotion ?

Class Promotion ?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 3 Posters 6.7k Views 3 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.
  • W Offline
    W Offline
    Walux
    wrote on last edited by
    #1

    Hi Qt Community ,

    The answer i'm afraid can be very obvious , but code-safety is priority .

    I have a class A , inherited by a class B.

    In a certain state , i want my class A to "promote" to class B .
    In other terms , i want the class A to "turn" into a class B that holds all the values of class A , and when i say "turn into" , i mean i want for the events that were occuring to the first instance of class A to still happen to the promoted class B.

    Thanks you very much.

    Taking things from beginning to end : That's my entertainment !

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      I didn't get it. Also I don't get how this is related to Qt.

      W 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        You have
        class B;
        class A : public B

        so A is always a B. ??

        so I dont get it either. :)

        besides it does remind me of
        http://stackoverflow.com/questions/274626/what-is-object-slicing

        W 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          You have
          class B;
          class A : public B

          so A is always a B. ??

          so I dont get it either. :)

          besides it does remind me of
          http://stackoverflow.com/questions/274626/what-is-object-slicing

          W Offline
          W Offline
          Walux
          wrote on last edited by Walux
          #4

          @mrjj

          It's actually :

          class A;

          class B : public A

          Class A has key and mouse events , and multiple protected variables.
          I want from my instance of class A to turn into a class B that contains copies of the values of the variables of class A while keeping the events working .

          Thanks for your help , imma take a look at your link

          Taking things from beginning to end : That's my entertainment !

          1 Reply Last reply
          0
          • ? A Former User

            I didn't get it. Also I don't get how this is related to Qt.

            W Offline
            W Offline
            Walux
            wrote on last edited by
            #5

            @Wieland

            To be more precise , both my classes A and B are subclassed QGraphicsItem ,
            they only differ in the implementation of the virtual paint() method .

            Taking things from beginning to end : That's my entertainment !

            mrjjM 1 Reply Last reply
            0
            • W Walux

              @Wieland

              To be more precise , both my classes A and B are subclassed QGraphicsItem ,
              they only differ in the implementation of the virtual paint() method .

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Walux
              so if u had both paint functions in same class and switched
              at some point, it would be A or B then?
              So if only diff is painting, then if u can switch painting, all be ok ?

              W 1 Reply Last reply
              0
              • mrjjM mrjj

                @Walux
                so if u had both paint functions in same class and switched
                at some point, it would be A or B then?
                So if only diff is painting, then if u can switch painting, all be ok ?

                W Offline
                W Offline
                Walux
                wrote on last edited by Walux
                #7

                @mrjj

                It would be in B's shape.

                @mrjj said:

                So if only diff is painting, then if u can switch painting, all be ok ?

                I thought about it , but i'm planning on making more derived classes that'll hold new variables.

                That 'switch' u mentionned is what i'm struggling to do.

                Taking things from beginning to end : That's my entertainment !

                mrjjM 1 Reply Last reply
                0
                • W Walux

                  @mrjj

                  It would be in B's shape.

                  @mrjj said:

                  So if only diff is painting, then if u can switch painting, all be ok ?

                  I thought about it , but i'm planning on making more derived classes that'll hold new variables.

                  That 'switch' u mentionned is what i'm struggling to do.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Walux
                  the simple version is
                  class Master
                  all variables..

                  class A : Master
                  paintEvent

                  class B : Master
                  paintEvent

                  the more lame is
                  class A :B
                  PaintAsA(QPainter *)
                  PaintAsB(QPainter *)

                  paintevent(...)
                  if(UseB)
                  PaintAsB
                  else
                  PaintAsA

                  which is bad with more sublasses

                  W 1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    Why do your shapes of class A and B have to be related at all? When ever you have an object of A on your scene and you want to "turn it into" an object of B, just create a new object of B, copy the relevant data from A to B and destroy the original A object.

                    W 1 Reply Last reply
                    1
                    • ? A Former User

                      Why do your shapes of class A and B have to be related at all? When ever you have an object of A on your scene and you want to "turn it into" an object of B, just create a new object of B, copy the relevant data from A to B and destroy the original A object.

                      W Offline
                      W Offline
                      Walux
                      wrote on last edited by
                      #10

                      @Wieland

                      By doing that , i lose all the key and mouse events that happen to class A.

                      Taking things from beginning to end : That's my entertainment !

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        You mean events that have been sent to A but haven't been "delivered" to it yet?

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          Walux
                          wrote on last edited by
                          #12

                          Yes , how can i assign all of them to the new class B ?

                          Taking things from beginning to end : That's my entertainment !

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            Call QCoreApplication::processEvents before destroying A.

                            W 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @Walux
                              the simple version is
                              class Master
                              all variables..

                              class A : Master
                              paintEvent

                              class B : Master
                              paintEvent

                              the more lame is
                              class A :B
                              PaintAsA(QPainter *)
                              PaintAsB(QPainter *)

                              paintevent(...)
                              if(UseB)
                              PaintAsB
                              else
                              PaintAsA

                              which is bad with more sublasses

                              W Offline
                              W Offline
                              Walux
                              wrote on last edited by
                              #14

                              @mrjj

                              Maybe i should do this instead :

                              I start by creating a class B that uses the paintEvent of class A , then when i reach the certain state , it calls the paintEvent of class B .

                              I have no problem doing it now , since the difference is only in the painting , but later when the new classes have new variables , that'll be a problem .

                              Taking things from beginning to end : That's my entertainment !

                              1 Reply Last reply
                              0
                              • ? A Former User

                                Call QCoreApplication::processEvents before destroying A.

                                W Offline
                                W Offline
                                Walux
                                wrote on last edited by
                                #15

                                @Wieland

                                Maybe a small piece of code will help me understand better , and thank you :)

                                Taking things from beginning to end : That's my entertainment !

                                1 Reply Last reply
                                0
                                • W Offline
                                  W Offline
                                  Walux
                                  wrote on last edited by
                                  #16

                                  I'm sure the answer i'm looking for lays somewhere between :

                                  • dynamic_cast
                                  • virtual
                                  • operator=

                                  Taking things from beginning to end : That's my entertainment !

                                  1 Reply Last reply
                                  0
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    I dont understand why it ever need to change "type".
                                    I think you need to rethink the design.
                                    Normally u use base class pointers and childs for polymorph operations but
                                    i cannot understand your use case.

                                    Its like circle that must sudden draw as a rect?

                                    W 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      I dont understand why it ever need to change "type".
                                      I think you need to rethink the design.
                                      Normally u use base class pointers and childs for polymorph operations but
                                      i cannot understand your use case.

                                      Its like circle that must sudden draw as a rect?

                                      W Offline
                                      W Offline
                                      Walux
                                      wrote on last edited by Walux
                                      #18

                                      @mrjj
                                      @Wieland

                                      I'll try to minimize the code , to help clarify my situation :

                                      Class myScene : public QGraphicsScene
                                      {
                                      //...
                                      void MousePressEvent(QGraphicsSceneMouseEvent *event)
                                      //...
                                      private:
                                      A *a;
                                      //...
                                      }
                                      

                                      "myScene.cpp"

                                      myScene::myScene 
                                      {
                                      a = new A;
                                      }
                                      
                                      void  myScene::MousePressEvent(QGraphicsSceneMouseEvent *event)
                                      {
                                      a->doesSmth();
                                      if(a->reachesSomePoint())
                                      a = new B;   // Compiles good but i lose the events and the whole paint of a;
                                      }
                                      
                                      Class A : public QObject , public QGraphicsItem
                                      {
                                      //...
                                          A();
                                          QRectF boundingRect() const Q_DECL_OVERRIDE;
                                          QPainterPath shape() const Q_DECL_OVERRIDE;
                                          void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                                                     QWidget *widget) Q_DECL_OVERRIDE; // This is the first painting
                                      
                                          
                                      //...
                                            protected:
                                      qreal myValue1;
                                      int myValue2;
                                      //...
                                      }
                                      
                                      Class B : public Class A
                                      {
                                              void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                                                     QWidget *widget) Q_DECL_OVERRIDE; // This is the second painting
                                      //Future Variables
                                             private:
                                      qreal futureValue1;
                                      //...
                                      }
                                      

                                      Taking things from beginning to end : That's my entertainment !

                                      1 Reply Last reply
                                      0
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        hi
                                        You only need to go from base to child?
                                        You can use a cast for that.
                                        http://www.bogotobogo.com/cplusplus/upcasting_downcasting.php

                                        and you dont lose A paint as you are free to call the A::paintEvent from
                                        B::paintEvent.

                                        W 1 Reply Last reply
                                        1
                                        • mrjjM mrjj

                                          hi
                                          You only need to go from base to child?
                                          You can use a cast for that.
                                          http://www.bogotobogo.com/cplusplus/upcasting_downcasting.php

                                          and you dont lose A paint as you are free to call the A::paintEvent from
                                          B::paintEvent.

                                          W Offline
                                          W Offline
                                          Walux
                                          wrote on last edited by
                                          #20

                                          @mrjj

                                          Thanks for your help , glad you stayed with me till the bottom ;)
                                          Imma read the content of your link :)

                                          Taking things from beginning to end : That's my entertainment !

                                          1 Reply Last reply
                                          1

                                          • Login

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