QGraphicsItem is not selectable after selecting one other QGraphics Item
-
Hi, I am currently trying to implementing a node editor for myself. But I have a semantic error that I couldnt figure it out. In my code there is a graphicsview and when adding item to the view everythin is fine. After selecting one item retrieves problem that I can not detect other nodes.
Can anyonde help me about it?
This is how I add nodes to view.

This is Nodes added to the application.

View Class Code
void NodeClass::mousePressEvent(QMouseEvent *event) { if(event->button()==Qt::RightButton){ QMenu menu(this); menu.addAction("Add Node"); menu.addAction("Add Node"); QPointF scenePoint = mapToScene(event->pos()); menu.exec(event->globalPos()); Node *myNode = new Node(scene,"Undefined Title"); myNode->setPos(scenePoint); //QGraphicsRectItem *button1 = new QGraphicsRectItem(scenePoint.x(),scenePoint.y(), 100, 200); //blockList->append(*myNode); scene->addItem(myNode); //qInfo()<<"This is items scene"<<scene->items(); }else{ QGraphicsView::mousePressEvent(event); // this is for avoiding the double clicks } }This is code of the mouse event of QGraphicsItem
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event){ int xpos = this->pos().x(); int ypos = this->pos().y(); int mapped_xpos = mapToScene(event->pos()).x() ; int mapped_ypos = mapToScene(event->pos()).y() ; if(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+height && event->button()==Qt::LeftButton){ // Printing the position of the mouse click qDebug() << "Mouse Clicked at: " << mapped_xpos << " " << mapped_ypos; // Printing the position of the node qDebug() << "Node Position: " << xpos << " " << ypos; // Printing the position of the node qDebug() << "Node Size: " << width << " " << height; // Printing the position of the node qDebug() << "Node Edge Size: " << edge_size; // Printing the position of the node qDebug() << "Node has choosen!"; // change the cursor to a holding hand }else{ } }This is console output
Mouse Clicked at: 371 296 Node Position: 255 120 Node Size: 340 480 Node Edge Size: 35 Node has choosen! // But after clicking another node it has the same focus on the QGraphicsItem lastly clicked.Thanks for your helps.
-
Hi, I am currently trying to implementing a node editor for myself. But I have a semantic error that I couldnt figure it out. In my code there is a graphicsview and when adding item to the view everythin is fine. After selecting one item retrieves problem that I can not detect other nodes.
Can anyonde help me about it?
This is how I add nodes to view.

This is Nodes added to the application.

View Class Code
void NodeClass::mousePressEvent(QMouseEvent *event) { if(event->button()==Qt::RightButton){ QMenu menu(this); menu.addAction("Add Node"); menu.addAction("Add Node"); QPointF scenePoint = mapToScene(event->pos()); menu.exec(event->globalPos()); Node *myNode = new Node(scene,"Undefined Title"); myNode->setPos(scenePoint); //QGraphicsRectItem *button1 = new QGraphicsRectItem(scenePoint.x(),scenePoint.y(), 100, 200); //blockList->append(*myNode); scene->addItem(myNode); //qInfo()<<"This is items scene"<<scene->items(); }else{ QGraphicsView::mousePressEvent(event); // this is for avoiding the double clicks } }This is code of the mouse event of QGraphicsItem
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event){ int xpos = this->pos().x(); int ypos = this->pos().y(); int mapped_xpos = mapToScene(event->pos()).x() ; int mapped_ypos = mapToScene(event->pos()).y() ; if(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+height && event->button()==Qt::LeftButton){ // Printing the position of the mouse click qDebug() << "Mouse Clicked at: " << mapped_xpos << " " << mapped_ypos; // Printing the position of the node qDebug() << "Node Position: " << xpos << " " << ypos; // Printing the position of the node qDebug() << "Node Size: " << width << " " << height; // Printing the position of the node qDebug() << "Node Edge Size: " << edge_size; // Printing the position of the node qDebug() << "Node has choosen!"; // change the cursor to a holding hand }else{ } }This is console output
Mouse Clicked at: 371 296 Node Position: 255 120 Node Size: 340 480 Node Edge Size: 35 Node has choosen! // But after clicking another node it has the same focus on the QGraphicsItem lastly clicked.Thanks for your helps.
@canrollas
I'm not sure what you mean by "selectable". You don't seem to use QGraphicItem's standard facility for that (using QGraphicsItem::ItemIsSelectable flag) -
@canrollas
I'm not sure what you mean by "selectable". You don't seem to use QGraphicItem's standard facility for that (using QGraphicsItem::ItemIsSelectable flag)@Asperamanca said in QGraphicsItem is not selectable after selecting one other QGraphics Item:
QGraphicsItem::ItemIsSelectable flag
Hi sir, When I mean selectable => When mouse clicks on it. After click I can detect mouse click and positions. BUt I can not detect other clicks in other Items. ( And thankyou for remind me Selectable Flag ) Do you have opinion about that?
-
@Asperamanca said in QGraphicsItem is not selectable after selecting one other QGraphics Item:
QGraphicsItem::ItemIsSelectable flag
Hi sir, When I mean selectable => When mouse clicks on it. After click I can detect mouse click and positions. BUt I can not detect other clicks in other Items. ( And thankyou for remind me Selectable Flag ) Do you have opinion about that?
@canrollas
First, the event has a scenePos, so you don't need to map the coordinates yourself.
Second, whyif(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+heightIf the mouse is not on the item, you simply won't get an event anyway
-
@canrollas
First, the event has a scenePos, so you don't need to map the coordinates yourself.
Second, whyif(xpos<=mapped_xpos && mapped_xpos<=xpos+width && ypos<=mapped_ypos && mapped_ypos<=ypos+heightIf the mouse is not on the item, you simply won't get an event anyway
@Asperamanca thanks for your warning it works that way but Your answer does not solve my problem.