Congratulations to our 2022 Qt Champions!

Expansion of Item in QGraphicsItem?

  • Hello everyone,
    I want to expand my item which is root item, but I don't how to call expandItem method and get QGraphicsItem item for it.
    What exactly happening in my code and what I want is as follows:

    • I have a class called ImportXML.cpp in that class I am calling another class called ModelScene.cpp to draw one item element for example square by this methode
    void ModelScene::CreateItemFromXml(QString nodeName, QPointF scenepos )
    • In ModelScene.cpp there is another method called
    void ModelScene::expandItem(QGraphicsItem *parentItem, int size = 1)
    	ModelItemExp *itemExp;
    	itemExp = new ModelItemExp(myItemType, size);
    	if(myItemType == ModelItem::Tuple) itemExp->setBrush(Qt::lightGray);
    		else itemExp->setBrush(Qt::white);
    	emit itemInserted(itemExp);
    	itemExp->setFlag(QGraphicsItem::ItemIsMovable, false);

    which is responsible for expanding element. in this method parameter is parentItem. but I dont know how to pass this parentItem from my ImportXML.class?

    • and also in mainWindow there is another method for handling expansion but how to join this three class so that when I call createItemXml method from ImportXML.class it first draw a square and then expand that square with expandItem method from ImportXML class only.
      mainWindow class code is as follows for handling expansion:
    void MainWindow::handleExpansion(int expSize, int direction){
    	foreach (QGraphicsItem *item, scene->selectedItems()) {
    			scene->expandItem(item, expSize);}
    	QGraphicsItem *parentItem = scene->selectedItems().last();
    	QGraphicsItem *item = parentItem->childItems().first();
    	int x; int y; int size;
    	if (direction==1){			// right
    		size = 0;
    		x = 250; y = 0;
    	else if (direction==2){		// top
    		size = item->boundingRect().height()*0.5;
    		x = 0; y = -250;
    	else if (direction==3){		// bottom
    		size = item->boundingRect().height()*0.5;
    		x = 0; y = 250;
    	item->setPos(x, y);
    	if (direction!=1) item->moveBy(size-100, 0);
    	scene->drawLine(parentItem, direction);

    Many Thank in Advance

  • Moderators

    i don't quite understand your problem.
    Your ImportXML class takes a XML definition and creates QGraphicsItems from it? Am i right?
    And you want to know what QgraphicsItem was created for a specific XML element?

  • @raven-worx ImportXML class is XML definition i.e reading of XML and inside ModelScene class I have method called CreateItemXML which can draw element graphically for me...

  • Lifetime Qt Champion

    Hi. To call function in main, you could
    define a new signal in your xml class and hook up to the
    mainwindows slots.
    That way you can activate methods from your XML class
    without having direct access to mainwindow.

  • @mrjj Thank you dear

Log in to reply