Results 1 to 5 of 5

Thread: Add object to a scene from a sub class

  1. #1
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Add object to a scene from a sub class

    Hello,

    i have create a dialog project with the Qt Creator which contains a code is as follows:


    dialog.h:

    Qt Code:
    1. class Dialog;
    2. }
    3.  
    4. class Dialog : public QDialog
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit Dialog(QWidget *parent = 0);
    10. ~Dialog();
    11.  
    12. private:
    13. Ui::Dialog *ui;
    14.  
    15.  
    16. MemberClass mClass;
    17. };
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. Dialog::Dialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene = new QGraphicsScene(this);
    8. ui->graphicsView->setScene(scene);
    9. }
    To copy to clipboard, switch view to plain text mode 

    From this point it is possible to add items with " scene->addItem(...)" to the scene.

    How it is possible to add items to the scene from outside of the dialog class?

    For Example:

    memberClass.h
    Qt Code:
    1. class MemberClass
    2. {
    3.  
    4. public:
    5. void addtoScene();
    6. };
    To copy to clipboard, switch view to plain text mode 


    memberClass.cpp
    Qt Code:
    1. void MemberClass::addtoScene()
    2. {
    3. //Here an item should be add to the scene of the dialog class
    4. }
    To copy to clipboard, switch view to plain text mode 

    Thx,
    Andre

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add object to a scene from a sub class

    You can either make the scene accessible, or you add "additem" methods to your dialog class and call those.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add object to a scene from a sub class

    ok, i got one solution, but i think it is not an elegant one.
    The static construct helps in this point.

    dialog.h
    Qt Code:
    1. class Dialog;
    2. }
    3.  
    4. class Dialog : public QDialog
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit Dialog(QWidget *parent = 0);
    10. ~Dialog();
    11.  
    12. private:
    13. Ui::Dialog *ui;
    14.  
    15. static QGraphicsScene *scene;
    16.  
    17. MemberClass mClass;
    18. };
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp

    Qt Code:
    1. QGraphicsScene* Dialog::scene = NULL;
    2.  
    3. Dialog::Dialog(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::Dialog)
    6. {
    7. ui->setupUi(this);
    8.  
    9. scene = new QGraphicsScene(this);
    10. ui->graphicsView->setScene(scene);
    11. }
    To copy to clipboard, switch view to plain text mode 

    MemberClass.cpp
    Qt Code:
    1. void MemberClass::addtoScene()
    2. {
    3. Dialog::scene->addItem(newItem);
    4. }
    To copy to clipboard, switch view to plain text mode 

    The problem is to get the the specific object of the dialog class.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add object to a scene from a sub class

    Quote Originally Posted by Andre008 View Post
    The problem is to get the the specific object of the dialog class.
    Just pass the pointer of the dialog class or the pointer of the scene to the mClass object, e.g. in its constructor.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Andre008 (24th February 2016)

  6. #5
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add object to a scene from a sub class

    Ok,t i got it,
    Thx

Similar Threads

  1. add custom class object to QVariantList
    By ggdev001 in forum Qt Programming
    Replies: 17
    Last Post: 26th February 2013, 07:16
  2. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  3. Problem in using UI object in other class
    By beginQT in forum Qt Programming
    Replies: 4
    Last Post: 19th January 2012, 11:53
  4. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 15:59
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.