Results 1 to 10 of 10

Thread: Cant access to superclass ...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Cant access to superclass ...

    I have a G_item_base class, it has some properties I want to use at G_item_rect (a QGraphicsRect subclassed ) and others...
    In example, I have this method:

    Qt Code:
    1. A_GItem_base::A_GItem_base(qint64 internal_pointer, WW::Graph_type my_type) {
    2. w_internal_type= QGraphicsItem::UserType + my_type; }
    3.  
    4. void A_GItem_base::w_set_pen(QPen my_pen ) {
    5. ((QAbstractGraphicsShapeItem*)this->metaObject()->superClass())->setPen(my_pen); }
    To copy to clipboard, switch view to plain text mode 
    And the G_item_rect is declared as:
    Qt Code:
    1. A_GItem_rect::A_GItem_rect (qint64 internal_pointer QGraphicsScene *scene, QGraphicsItem *parent ) : QGraphicsRectItem (parent,scene) , A_GItem_base(internal_pointer, WW::Graph_rectangle)
    To copy to clipboard, switch view to plain text mode 

    Ok, I can't use Q_object macro. I have
    " Class contains Q_OBJECT macro but does not inherit from QObject"

    Any help ? Thanks

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    520
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 77 Times in 75 Posts

    Default Re: Cant access to superclass ...

    Hi, you don't need the Q_OBJECT macro in the class if it doesn't use signals and slots.

    Ginsengelf

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: Cant access to superclass ...

    Ok, ... What casting can i do .... I have tested all...

    ((QAbstractGraphicsShapeItem *) this)->setPen(my_pen);
    QAbstractGraphicsShapeItem * r = reinterpret_cast<QAbstractGraphicsShapeItem*>(this ) ;

    I have crash for both ...

    Thanks.
    Last edited by tonnot; 23rd November 2011 at 11:21.

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    18
    Thanked 68 Times in 66 Posts

    Default Re: Cant access to superclass ...

    please post full class declaration

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: Cant access to superclass ...

    Here you are :

    Qt Code:
    1. class A_GItem_base {
    2. public:
    3. A_GItem_base(WW::Graph_type my_type);
    4. void w_set_pen(QPen my_pen );
    5. };
    6.  
    7. A_GItem_base::A_GItem_base( WW::Graph_type my_type) { }
    8.  
    9. void A_GItem_base::w_set_pen(QPen my_pen ) {
    10. // or
    11. QAbstractGraphicsShapeItem * r = reinterpret_cast<QAbstractGraphicsShapeItem*>(this) ;
    12. r->setPen(my_pen);
    13. // or
    14. ((QAbstractGraphicsShapeItem *) this)->setPen(my_pen);
    15. // both crashes
    16. }
    17.  
    18. class A_GItem_rect : public QGraphicsRectItem , public A_GItem_base {
    19. public:
    20. A_GItem_rect(QGraphicsScene *scene = 0, QGraphicsItem *parent = 0 );
    21. void w_set_rect( QRect rectf) ;
    22. };
    23.  
    24. A_GItem_rect::A_GItem_rect (QGraphicsScene *scene, QGraphicsItem *parent ) : QGraphicsRectItem (parent,scene) , A_GItem_base(WW::Graph_rectangle) { }
    25. void A_GItem_rect::w_set_rect( QRect rectf) { this->setRect(rectf); }
    To copy to clipboard, switch view to plain text mode 

    I have :
    A_GItem_rect * my_rect = new A_GItem_rect(this->scene()) ;
    my_rect->w_set_pen(a_pen);


    I'm trying to do this strange casting because I dont know how to access to the superclass of A_GItem_Base ...


    Thanks
    Last edited by tonnot; 23rd November 2011 at 12:10.

  6. #6
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    520
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 77 Times in 75 Posts

    Default Re: Cant access to superclass ...

    Hi, do you really need the w_set_pen() method in A_GItem_base? Because if you remove it and call setPen() on your instance of A_GItem_Rect, the method of the base class should be called.

    Ginsengelf

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Cant access to superclass ...

    Quote Originally Posted by tonnot View Post
    Ok, I can't use Q_object macro. I have
    " Class contains Q_OBJECT macro but does not inherit from QObject"

    Any help ? Thanks
    The error message tells you exactly what the problem is. The Q_OBJECT macro installs some support code for the Qt meta-object system. A_GItem_base is not derived from QObject and therefore does not support anything that needs the Qt meta-object extensions... including signals and slots.

  8. #8
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: Cant access to superclass ...

    Ginsengelf:
    Yes I need to have control on this. (I know I can use setpen()... ) .

    Chris: Yes, I understand. I wanted to use metaobject to discover the superclass.

    By now, I have w_set_pen for every class....

    Class A
    Class B : extending A and Z
    Class C : extending A and Z
    ...
    Class M : extending A and Z

    I need to set or use a Z property/method from A. And I need to check it and other things.
    So.... How can I, inside A , have access to Z inherited methods of my superclass (B, C, or any ).
    Thanks for your patience

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: Cant access to superclass ...

    Why not trying with composition instead of inheritance:
    Qt Code:
    1. class A_GItem_base {
    2. public:
    3. A_GItem_base(WW::Graph_type my_type) : _itemPtr(NULL){
    4. }
    5. void w_set_pen(QPen my_pen ){
    6. if( _itemPtr ) _itemPtr->setPen(my_pen);
    7. }
    8. // here you can implement all methods that will use the QGraphicsItem class methods
    9. protected:
    10. QGraphicsItem * _itemPtr;
    11. };
    12.  
    13. class A_GItem_rect : public A_GItem_base {
    14. public:
    15. A_GItem_rect(QGraphicsScene *scene = 0, QGraphicsItem *parent = 0 ) : A_GItem_base(WW::Graph_rectangle){
    16. this->_itemPtr = new QGraphicsRectItem(parent,scene);
    17. }
    18. void w_set_rect( QRect rectf){
    19. if( QGraphicsRectItem * r = dynamic_cast<QGraphicsRectItem*>(_itemPtr) )
    20. r->setRect(rectf);
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 

    If somehow in class "base" you need to know some data or methods specific to A_GItem_rect, then you should reconsider your design, and use virtual methods properly.

  10. The following user says thank you to stampede for this useful post:

    tonnot (24th November 2011)

  11. #10
    Join Date
    Sep 2010
    Posts
    654
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    56
    Thanked 1 Time in 1 Post

    Default Re: Cant access to superclass ...

    Ok, this point of view is really what I need.
    Thank you very much.

Similar Threads

  1. Access USB from Qt???
    By webquinty in forum Qt Programming
    Replies: 5
    Last Post: 6th June 2011, 08:40
  2. Qt Creator Can not access network using QT 4.7.2
    By jayesh_shah in forum Qt Tools
    Replies: 2
    Last Post: 12th April 2011, 05:36
  3. Can't access dll
    By waynew in forum Newbie
    Replies: 4
    Last Post: 15th November 2010, 13:06
  4. web access
    By ashwinibm in forum Newbie
    Replies: 1
    Last Post: 15th January 2010, 10:27
  5. Database access from Qt
    By nimmyj in forum Newbie
    Replies: 1
    Last Post: 23rd November 2006, 09:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.