QGraphicsItem paint not called
-
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(); -
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();Hi and welcome to devnet,
Any chances that scene is a function local variable ? This means it will be destroyed before being shown.
-
Hi and welcome to devnet,
Any chances that scene is a function local variable ? This means it will be destroyed before being shown.
@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); } } -
N nance97 has marked this topic as solved on
-
To the best of my knowledge there's no "connect these two blocks" prebuilt logic. You will have to implement it yourself.
-
To the best of my knowledge there's no "connect these two blocks" prebuilt logic. You will have to implement it yourself.