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. Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.
Forum Updated to NodeBB v4.3 + New Features

Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 762 Views 1 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.
  • ValdermanV Offline
    ValdermanV Offline
    Valderman
    wrote on last edited by
    #1

    I have the following project: I am creating a node editor.
    1.png
    In the first picture there are 2 types of objects, the nodes themselves, and a black stick that connects them - an edge.
    The node class looks like this:

    class Node : public QGraphicsObject
    

    I really need the meta information that is given from the QObject class. And as I understood, if I want to attach slots, I will still need to inherit from the QObject class.

    The Problem itself is in my EDGE, for the correct operation and calculations of this edge, I use inheritance from the QGraphicsPathItem class, but I also need meta information, so we get duplication in inheritance:

    class NodeEdge : public QGraphicsObject, public QGraphicsPathItem
    

    I found the following guide on the Internet and redefined all virtual methods, as in that guide.
    This is how it looks now:
    Node Edge.hpp
    NodeEdge.cpp

    The bug is: when I click on an edge, it should be selected and the color changed, so this happens only in the area positive in x and y (pic. 2), if at least one coordinate is negative, then the program behaves as if the object does not exist, but at the same time it is drawn properly.
    2.jpg
    In the second picture, the plus sign marks the area where the edge behaves normally, in accordance with the idea.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Since a QGraphicsItem can have children I don't see why it should be needed to derive from QGraphicsObject and QGraphicsPathItem.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • ValdermanV Offline
        ValdermanV Offline
        Valderman
        wrote on last edited by
        #3

        Do you mean that I need to create a QGraphicsObject object and pass an empty QGraphicsPathItem in the constructor, and then in my NodeEdge class call childrenBoundingRect() in the redefined boundingRect() method?

        Christian EhrlicherC 1 Reply Last reply
        0
        • ValdermanV Valderman

          Do you mean that I need to create a QGraphicsObject object and pass an empty QGraphicsPathItem in the constructor, and then in my NodeEdge class call childrenBoundingRect() in the redefined boundingRect() method?

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Valderman said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:

          Do you mean that I need to create a QGraphicsObject object and pass an empty QGraphicsPathItem in the constructor, and then in my NodeEdge class call childrenBoundingRect() in the redefined boundingRect() method?

          Since I don't know what you're trying to do - maybe.
          You can group QGraphicsItems and it looks like this is what you want to do.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • ValdermanV Offline
            ValdermanV Offline
            Valderman
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:

            Since I don't know what you're trying to do - maybe.

            In a nutshell, I want to create an object with the properties of QGraphicsObject and QGraphicsPathItem. In the future, I will interact with this object as QGraphicsObject, get its name, position and other meta-information. And at the same time it should be drawn as QGraphicsPathItem with setPath() or path() .This is where I got double inheritance and errors.
            Btw my code with this class is attached in the links to pastebin in first post.

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then use QGraphicsPathItem as a child item and pass all the stuff you need through your QGraphicsObject.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • ValdermanV Offline
                ValdermanV Offline
                Valderman
                wrote on last edited by
                #7

                To be honest, I couldn't do it with your method. But after writing the following line

                QGraphicsObject::setPos({-0.0001, -0.0001});
                

                in

                NodeEdge::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
                

                everything began to work as it should (I returned to the old version with double inheritance)
                Maybe you had any ideas why?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  If you just need the QObject part then your double inheritance is "wrong". QGraphicsObject is a class that inherits from QGraphicsItem and QObject so you can just make yours inherit from QGraphicsPathItem and QObject.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • ValdermanV Offline
                    ValdermanV Offline
                    Valderman
                    wrote on last edited by Valderman
                    #9

                    All my graphics object should be QGraphicsObject becаuse after all, I had that function:

                    QGraphicsObject* QDMGraphicsView::getItemAClick​(QMouseEvent *event)
                    ​{ 
                     ​    QPoint position = event->​pos​(); 
                     ​    QGraphicsObject* obj = ​nullptr​; 
                     ​    ​if​ (​itemAt​(position) != ​nullptr​) 
                     ​    { 
                     ​        obj = ​itemAt​(position)->​toGraphicsObject​(); 
                     ​    } 
                     ​    ​return​ obj; 
                     ​}
                    

                    It is pretty important in programm's logic. Idea is - I clicked at some graphics object and gets theit meta-information in cin for example. Whel all my classes inherits from QGraphicsItem - it's easy to do. But any ideas what I should do with class that inherits from QGraphicsPathItem?

                    JonBJ 1 Reply Last reply
                    0
                    • ValdermanV Valderman

                      All my graphics object should be QGraphicsObject becаuse after all, I had that function:

                      QGraphicsObject* QDMGraphicsView::getItemAClick​(QMouseEvent *event)
                      ​{ 
                       ​    QPoint position = event->​pos​(); 
                       ​    QGraphicsObject* obj = ​nullptr​; 
                       ​    ​if​ (​itemAt​(position) != ​nullptr​) 
                       ​    { 
                       ​        obj = ​itemAt​(position)->​toGraphicsObject​(); 
                       ​    } 
                       ​    ​return​ obj; 
                       ​}
                      

                      It is pretty important in programm's logic. Idea is - I clicked at some graphics object and gets theit meta-information in cin for example. Whel all my classes inherits from QGraphicsItem - it's easy to do. But any ideas what I should do with class that inherits from QGraphicsPathItem?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @Valderman said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:

                      Whel all my classes inherits from QGraphicsItem - it's easy to do. But any ideas what I should do with class that inherits from QGraphicsPathItem?

                      I don't understand this question, because QGraphicsPathItem already inherits from QGraphicsItem....

                      @SGaist said in Multiple Inheritance from QGraphicsObject and concrete QGraphics*item.:

                      If you just need the QObject part then your double inheritance is "wrong". QGraphicsObject is a class that inherits from QGraphicsItem and QObject so you can just make yours inherit from QGraphicsPathItem and QObject.

                      He is saying that if all you need from QGraphicsObject is the QObject inheritance (e.g. so you can do signals/slots) you might be better changing your code to do your own multiple inheritance from SomeGraphicsItem + QObject and work off QGraphicsItem + QObject base commonality instead of QGraphicsObject.

                      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