How to draw variable size square on fix position click on menu item import??



  • Hello everyone,
    I have diagram editor like the example (diagramscreen) provide by QT where items are drawn on the screen by mouse click event. In my program, I read XML file and all the element of it including root element. Which I am importing it into the Diagram editor. Now when I read root element I want to call diagramsceen class which can draw square for on screen the root element.
    My Problem is: what to pass to that class whether it is root.nodeName() or what? also how to find the position on the screen to draw that element (i.e. square).
    the method of drawscreen.class is as follow but with mouse click event ... I dont want mouse click event

    void drawscreen::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
        if (mouseEvent->button() != Qt::LeftButton)
            return;
    
    	//QList<QGraphicsItem *> addedItems;
    	//QGraphicsItemGroup group;
    	//QLabel label();
    
    	ModelItem *item;
        switch (myMode) {
            case InsertItem:
    
    			// Item
    			item = new ModelItem(myItemType, myItemMenu);
    			if(myItemType == ModelItem::Tuple) item->setBrush(Qt::lightGray);
    				else item->setBrush(Qt::white);
    			myPen.setColor(Qt::black);
    			myPen.setWidth(2);
    			item->setPen(myPen);
    			addItem(item);
                item->setPos(mouseEvent->scenePos());
                emit itemInserted(item);
    
    			// Identifier
    			textItem = new ModelTextItem();
    			textItem->setFont(myFont);
    			textItem->setScale(5);
    			textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
    			textItem->setZValue(1000.0);
    			connect(textItem, SIGNAL(lostFocus(ModelTextItem*)),
    					this, SLOT(editorLostFocus(ModelTextItem*)));
    			connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
    					this, SIGNAL(itemSelected(QGraphicsItem*)));
    			addItem(textItem);
    			textItem->setDefaultTextColor(myTextColor);
    			textItem->setParentItem(item);
    			textItem->setFlag(QGraphicsItem::ItemIsMovable, false);
    			textItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
    			if (myItemType == ModelItem::Relation){
    				textItem->setPos(-90, -80);
    				textItem->setTextWidth(36);
    			}
    			else if (myItemType == ModelItem::Mapping){
    				textItem->setPos(-90, -80);
    				textItem->setTextWidth(52);
    				textItem->setScale(3.5);
    			}
    			else if (myItemType == ModelItem::SetElement){
    				textItem->setPos(-90, -32);
    				textItem->setTextWidth(60);
    				textItem->setScale(3);
    			}
    			else{
    				textItem->setPos(-75, -50);
    				textItem->setTextWidth(30);
    			}
    			textItem->document()->setDefaultTextOption(QTextOption(Qt::AlignCenter));
    			emit textInserted(textItem);
    
    		break;
    
    		case InsertLine:
    			line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
    										mouseEvent->scenePos()));
    			line->setPen(QPen(myLineColor, 2));
    			addItem(line);
    			break;
    
    		case MoveItem:
    			break;
    
            case InsertText:
    			textItem = new ModelTextItem();
                textItem->setFont(myFont);
                textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
                textItem->setZValue(1000.0);
    			connect(textItem, SIGNAL(lostFocus(ModelTextItem*)),
    					this, SLOT(editorLostFocus(ModelTextItem*)));
                connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
                        this, SIGNAL(itemSelected(QGraphicsItem*)));
                addItem(textItem);
                textItem->setDefaultTextColor(myTextColor);
                textItem->setPos(mouseEvent->scenePos());
    			emit textInserted(textItem);
        default:
            ;
        }
        QGraphicsScene::mousePressEvent(mouseEvent);
    }
    
    

    Many Thanks in advance


  • Lifetime Qt Champion

    Hi,

    That's up to you to know. Do you have a set of items that matches the elements of your XML ? If so, then choose a way to pass that information, be it by string or using a enumeration etc.

    As for the position of the items, again, it's up to you to decide.



  • @SGaist Can I pass root.nodeName(); which return me name of my root element and also how to get rid of this item->setPos(mouseEvent->scenePos()); I dont want mouse event I want when I pass my root element it should directly draw square for me.
    Is this correct to write method like this :
    void drawscreen::importXML(ModelItem::ModelType myItemtype){
    }
    in this myItemtype is enum. but Can enum but name of root element

    Many Thanks dear


  • Qt Champions 2016

    Hi
    Create a new function with some of the code from mousePressEvent
    Lets call it
    CreateItemFromXml(QString Name, DiagramItem::DiagramType Type, QPointF InsertPos )

    Then u just call this from where u read the xml.

    you will need to map your string name to the enum type using some ifs.

    Also
    for each call, you should increase InsertPos .y so not all are on top.



  • @mrjj Thank you so much dear :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.