Results 1 to 10 of 10

Thread: How to create a "node" using QObject

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: How to create a "node" using QObject

    Could you explain what you meant by the "Add connection child" comment?
    It means "write the code here that you need to insert the child node into whatever data structure you are using to hold the information in the tree you are building".

    If your CFCAbstract hierarchy is all QObject-based (i.e. CFCAbstract inherits from QObject) then this could be as simple as:

    Qt Code:
    1. bool CFCInterface::addChild( CFCAbstract * pChild )
    2. {
    3. CFCConnection * pConn = qobject_cast< CFCConnection * >( pChild );
    4. if ( pConn != nullptr )
    5. {
    6. pConn->setParent( this );
    7. return true;
    8. }
    9. return false;
    10. }
    To copy to clipboard, switch view to plain text mode 

    But if I were doing this, I would implement a customized tree model that stored this structure independently of the view, then wrap it into a QAbstractItemModel for display in a QTreeView. See the Qt Simple Tree Model example.

    To translate that example to yours, "TreeItem" is replaced everywhere by "CFCAbstract", and all of the specific types derive from the CFCAbstract base class (which itself no longer derives from QObject; it's just an ordinary C++ class). The appendChild() method is what I called "addChild()" above, and would have to be made a virtual method and reimplemented in each class derived from CFCAbstract in order to implement your rules for what can be added where.
    Last edited by d_stranz; 12th May 2017 at 06:59.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    cryomicl (12th May 2017)

  3. #2
    Join Date
    May 2017
    Posts
    10
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows
    Thanks
    2

    Default Re: How to create a "node" using QObject

    @d_stranz
    Thank you so very much!! As you can see, my knowledge in Qt is fairly limited. But with the help of supportive experts like you, people like me will surely learn!

  4. #3
    Join Date
    May 2017
    Posts
    10
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows
    Thanks
    2

    Default Re: How to create a "node" using QObject

    @d_stranz
    I tried implementing the code, but there was no change in the GUI. By using breakpoints I found that the qobjectcast was returning a null pointer (pConn ==nullptr). Could you take a stab at why this could possibly be happening?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: How to create a "node" using QObject

    Is the CFCConnection class derived from QObject somewhere in its inheritance hierarchy? Is the Q_OBJECT macro present in the class definition in the header file? Is MOC being run on the header file?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    May 2017
    Posts
    10
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows
    Thanks
    2

    Default Re: How to create a "node" using QObject

    Quote Originally Posted by d_stranz View Post
    Is the CFCConnection class derived from QObject somewhere in its inheritance hierarchy? Is the Q_OBJECT macro present in the class definition in the header file? Is MOC being run on the header file?
    Yes, yes and yes.

    The code to demonstrate the first two points:
    Qt Code:
    1. #include <QObject> //Included QObject
    2. #include "ResourceItem.h"
    3. #include "MonWindow.h"
    4. #include "FCTab.h"
    5. #include "ResourceItem.h"
    6. #include "FCAbstract.h"
    7. #include "FCInterface.h"
    8.  
    9. class CFCConnections: public CResourceItem
    10. {
    11.  
    12. Q_OBJECT //QObject MACRO
    13.  
    14. public:
    15. CFCConnections(QObject*);
    16. ~CFCConnections();
    17.  
    18. private:
    19. CFCInterface* pParent;
    20.  
    21.  
    22. };
    23.  
    24.  
    25. #endif
    To copy to clipboard, switch view to plain text mode 

    That was the FCConnections.h code. And I know that MOC has run on the header file because I had to manually change its build step to include MOC. This image below shows the inheritance hierarchy. CFCConnections is derived from CResourceItem which has QObject as its base class.
    1.jpg

    POSSIBLE SOLUTION

    I just had to change the line
    Qt Code:
    1. CFCConnections* f_pConn=0;
    To copy to clipboard, switch view to plain text mode 

    to
    Qt Code:
    1. CFCConnections* f_pConn;
    2. f_pConn = new CFCConnections(this);
    To copy to clipboard, switch view to plain text mode 
    Last edited by cryomicl; 16th May 2017 at 09:17.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: How to create a "node" using QObject

    I just had to change the line
    So you mean to say you were trying to add a NULL pointer to an instance of CFCConnection to your hierarchy? Of course that won't work. qobject_cast<> won't magically turn a NULL or uninitialized pointer into a real one, it turns a real pointer to some QObject-based class into a pointer to another QObject-based class if the conversion is valid (that is, the type you are trying to cast to inherits from the type that is passed in).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #7
    Join Date
    May 2017
    Posts
    10
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows
    Thanks
    2

    Default Re: How to create a "node" using QObject

    Quote Originally Posted by d_stranz View Post
    Of course that won't work. qobject_cast<> won't magically turn a NULL or uninitialized pointer into a real one.
    Now I know this from experience, the best way to never forget a lesson.
    Thank you for all your help

Similar Threads

  1. Replies: 2
    Last Post: 24th December 2015, 14:33
  2. Replies: 1
    Last Post: 20th November 2015, 11:02
  3. Replies: 3
    Last Post: 16th March 2015, 08:31
  4. Replies: 2
    Last Post: 27th January 2012, 18:29
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05

Tags for this Thread

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.