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. QGraphicsItem paint not called
Qt 6.11 is out! See what's new in the release blog

QGraphicsItem paint not called

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 561 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.
  • N Offline
    N Offline
    nance97
    wrote on last edited by
    #1

    Hello,

    I'm trying to build a tree view for a user-defined class called Family_tree with nodes having a pointer to their father, mother, spouse and a vector of children. I created a TreeViewNode class derived from QGraphicsItem with a node* m_node member and overrides for the boundingRect and paint methods like so :

    QRectF TreeViewNode::boundingRect() const {
        return QRectF(-50, -50, 100, 100);
    }
    
    void TreeViewNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
        qDebug() << "Paint function called";
        painter->drawRect(boundingRect());
        painter->drawText(boundingRect(), Qt::AlignCenter, QString::fromStdString(m_node->getPatient().get_Name()));
    
        for (auto child : m_node->getChildren()) {
            TreeViewNode* childNode = new TreeViewNode(child, this);
            childNode->setPos(0, 100);
            QLineF line(0, 50, 0, 150);
            painter->drawLine(line);
        }
    }
    

    I've set up a Family_tree with just one node for simplicity (the root) and I'm trying to get it to show in the view in MainWindow but I don't see paint getting called at all despite me adding the root to the scene (scene.item().count() returns 1..) and the view is simply blank. I'm very much new to this so I would really appreciate some insight, thank you for your time!

    TreeViewNode* root = new TreeViewNode(family->get_root());
    QGraphicsScene scene; scene.addItem(root); root->setPos(-100, -100);
    view.setScene(&scene); view.show();
    
    SGaistS 1 Reply Last reply
    0
    • N nance97

      Hello,

      I'm trying to build a tree view for a user-defined class called Family_tree with nodes having a pointer to their father, mother, spouse and a vector of children. I created a TreeViewNode class derived from QGraphicsItem with a node* m_node member and overrides for the boundingRect and paint methods like so :

      QRectF TreeViewNode::boundingRect() const {
          return QRectF(-50, -50, 100, 100);
      }
      
      void TreeViewNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
          qDebug() << "Paint function called";
          painter->drawRect(boundingRect());
          painter->drawText(boundingRect(), Qt::AlignCenter, QString::fromStdString(m_node->getPatient().get_Name()));
      
          for (auto child : m_node->getChildren()) {
              TreeViewNode* childNode = new TreeViewNode(child, this);
              childNode->setPos(0, 100);
              QLineF line(0, 50, 0, 150);
              painter->drawLine(line);
          }
      }
      

      I've set up a Family_tree with just one node for simplicity (the root) and I'm trying to get it to show in the view in MainWindow but I don't see paint getting called at all despite me adding the root to the scene (scene.item().count() returns 1..) and the view is simply blank. I'm very much new to this so I would really appreciate some insight, thank you for your time!

      TreeViewNode* root = new TreeViewNode(family->get_root());
      QGraphicsScene scene; scene.addItem(root); root->setPos(-100, -100);
      view.setScene(&scene); view.show();
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Any chances that scene is a function local variable ? This means it will be destroyed before being shown.

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

      N 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        Any chances that scene is a function local variable ? This means it will be destroyed before being shown.

        N Offline
        N Offline
        nance97
        wrote on last edited by
        #3

        @SGaist thank you! The issue was indeed scene being local to the MainWindow constructor. Can I be greedy and ask you another question if you don't mind? I set up the TreeViewNode constructor as follows so it adds the right number of nodes to the scene and positions them according to their relationships with one another but I'm not too sure about how to use paint() to actually draw the branches between them now.. is there a good method that immediately comes to mind or do I simply have to figure out how to use painter->drawLine() to add all the individual lines? Thank you!

        TreeViewNode::TreeViewNode(node* node, Family_tree* family, QGraphicsScene* scene, QGraphicsItem* parent) : QGraphicsItem(parent), m_node(node), m_family(family), m_scene(scene) {
            if (m_node == m_family->get_root()) {m_scene->addItem(this);}
            addedNodes.insert(m_node);
            qDebug() << "Scene item count: " << m_scene->items().count();
        
            if (m_node->getFather()) {
                auto father = m_node->getFather();
                if (addedNodes.count(father) > 0) {return;}
                TreeViewNode* fatherNode = new TreeViewNode(m_node->getFather(), family, m_scene, this);
                fatherNode->setPos(-75, -75);
            }
        
            if (m_node->getMother()) {
                auto mother = m_node->getMother();
                if (addedNodes.count(mother) > 0) {return;}
                TreeViewNode* motherNode = new TreeViewNode(m_node->getMother(), family, m_scene, this);
                motherNode->setPos(75, -75);
            }
        
            if (m_node->getSpouse()) {
                auto spouse = m_node->getSpouse();
                if (addedNodes.count(spouse) > 0) {return;}
                TreeViewNode* spouseNode = new TreeViewNode(m_node->getSpouse(), family, m_scene, this);
                spouseNode->setPos(150, 0);
            }
        
            int childCount = 0;
            for (auto child : m_node->getChildren()) {
                if (addedNodes.count(child) > 0) {return;}
                TreeViewNode* childNode = new TreeViewNode(child, family, m_scene, this);
                childCount++;
                childNode->setPos(childCount * 75, 150);
            }
        }
        
        1 Reply Last reply
        0
        • N nance97 has marked this topic as solved on
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          To the best of my knowledge there's no "connect these two blocks" prebuilt logic. You will have to implement it yourself.

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

          N 1 Reply Last reply
          0
          • SGaistS SGaist

            To the best of my knowledge there's no "connect these two blocks" prebuilt logic. You will have to implement it yourself.

            N Offline
            N Offline
            nance97
            wrote on last edited by
            #5

            @SGaist understood, thanks again!

            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