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. Organize/Align multiple QGraphicsItems in a Layout? QGraphicsScene

Organize/Align multiple QGraphicsItems in a Layout? QGraphicsScene

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt c++qlayoutitemqgraphicssceneqgraphicsitemqgraphicslayout
2 Posts 2 Posters 382 Views
  • 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
    StudentScripter
    wrote on 6 Jan 2024, 11:36 last edited by StudentScripter 1 Jun 2024, 11:37
    #1

    I know that somehow its needed to create a QGraphicsLinearLayout and inherit with my QGraphicsItem subclass from QGraphicsLayoutItem too. But somehow it doesn't looks like there is a layout created at all. I still can freely move my graphicsitems in the scene even when beeing added to the layout:

    
    CustomGraphicsScene::CustomGraphicsScene(QObject *parent)
        : QGraphicsScene(parent)
    {   
     // Create a layout and a widget to hold the items
        layout = new QGraphicsLinearLayout(Qt::Vertical);
        QGraphicsWidget *widget = new QGraphicsWidget;
        widget->setLayout(layout);
        addItem(widget);  
    }
    
    
    
    void CustomGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
    {
        if(event->mimeData()->property("Key").canConvert(QMetaType(QMetaType::Int))){
    
            int key = event->mimeData()->property("Key").toInt();
            
            switch(key){
            
                case 10: {
                clearSelection();
                ResizablePixmapItem * pixItem = new ResizablePixmapItem(QPixmap(":/resource/quick.png"));
                pixItem->setRect(0,0,100,100);
                pixItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsScenePositionChanges);
                addItem(pixItem);
                pixItem->setPos(event->scenePos() - QPointF(pixItem->boundingRect().width()/2, pixItem->boundingRect().height()/2));
                pixItem->setSelected(true);
                
                layout->addItem(pixItem);
                
    
    
    
                //Erhöht den Counter für jedes gedroppete Item
                SceneItemCounter++;
                pixItem->setSceneID(SceneItemCounter);
                qDebug() << "SceneID: " << pixItem->getSceneID();
                
               
                emit SendNewItemWasAddedtoScene(pixItem);
                emit SendItemsInSceneSelected();  
                }
                break;
    

    Does anyone know how to do it properly? My goal is to control the aligment and position of all qgraphicsitems within the layout.

    P 1 Reply Last reply 6 Jan 2024, 11:43
    0
    • S StudentScripter
      6 Jan 2024, 11:36

      I know that somehow its needed to create a QGraphicsLinearLayout and inherit with my QGraphicsItem subclass from QGraphicsLayoutItem too. But somehow it doesn't looks like there is a layout created at all. I still can freely move my graphicsitems in the scene even when beeing added to the layout:

      
      CustomGraphicsScene::CustomGraphicsScene(QObject *parent)
          : QGraphicsScene(parent)
      {   
       // Create a layout and a widget to hold the items
          layout = new QGraphicsLinearLayout(Qt::Vertical);
          QGraphicsWidget *widget = new QGraphicsWidget;
          widget->setLayout(layout);
          addItem(widget);  
      }
      
      
      
      void CustomGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
      {
          if(event->mimeData()->property("Key").canConvert(QMetaType(QMetaType::Int))){
      
              int key = event->mimeData()->property("Key").toInt();
              
              switch(key){
              
                  case 10: {
                  clearSelection();
                  ResizablePixmapItem * pixItem = new ResizablePixmapItem(QPixmap(":/resource/quick.png"));
                  pixItem->setRect(0,0,100,100);
                  pixItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsScenePositionChanges);
                  addItem(pixItem);
                  pixItem->setPos(event->scenePos() - QPointF(pixItem->boundingRect().width()/2, pixItem->boundingRect().height()/2));
                  pixItem->setSelected(true);
                  
                  layout->addItem(pixItem);
                  
      
      
      
                  //Erhöht den Counter für jedes gedroppete Item
                  SceneItemCounter++;
                  pixItem->setSceneID(SceneItemCounter);
                  qDebug() << "SceneID: " << pixItem->getSceneID();
                  
                 
                  emit SendNewItemWasAddedtoScene(pixItem);
                  emit SendItemsInSceneSelected();  
                  }
                  break;
      

      Does anyone know how to do it properly? My goal is to control the aligment and position of all qgraphicsitems within the layout.

      P Offline
      P Offline
      Pl45m4
      wrote on 6 Jan 2024, 11:43 last edited by
      #2

      @StudentScripter

      Have you checked the QGraphicsLayout example already?

      • https://doc.qt.io/qt-6/qtwidgets-graphicsview-basicgraphicslayouts-example.html

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1

      2/2

      6 Jan 2024, 11:43

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved