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. How to draw variable size square on fix position click on menu item import??
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.0k Views 2 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.
  • S Offline
    S Offline
    ShrikantAmbade
    wrote on last edited by
    #1

    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

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

      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.

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

      S 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        S Offline
        S Offline
        ShrikantAmbade
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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.

          S 1 Reply Last reply
          1
          • mrjjM mrjj

            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.

            S Offline
            S Offline
            ShrikantAmbade
            wrote on last edited by
            #5

            @mrjj Thank you so much dear :)

            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