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 add a graphicsrectitem to a graphicslayout?
QtWS25 Last Chance

How to add a graphicsrectitem to a graphicslayout?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt c++qgraphicswidgetqgraphicslayout
5 Posts 3 Posters 421 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 20 Mar 2024, 07:32 last edited by
    #1

    Can't find much information on how to achieve that, so any hint is appreciated. :D
    My goal is to have all items in a layout with adjustable margins between the items. The graphicsitems themself should still be scalable in height if the layout is horizontal and scalable in width if the layout is vertical.

            // Erstellen Sie ein QGraphicsWidget
            QGraphicsWidget *widget = new QGraphicsWidget();
            widget->setFlag(QGraphicsItem::ItemIsMovable);
            QGraphicsWidget *item1 = new QGraphicsWidget;
            item1->setAutoFillBackground(true);
    
            QGraphicsWidget *item2 = new QGraphicsWidget;
            item2->setAutoFillBackground(true);
            // Erstellen Sie ein Layout
            
            QGraphicsRectItem *RectItem = new QGraphicsRectItem();
            QGraphicsLinearLayout *layout = new QGraphicsLinearLayout();
            
            
    
    
            layout->addItem(item1);
            layout->addItem(RectItem);
            layout->addItem(item2);
    
              layout->setSpacing(50);
             layout->setOrientation(Qt::Vertical);
            // Setzen Sie das Layout für das Widget
            widget->setLayout(layout);
    
            // Fügen Sie das Widget zur Szene hinzu
            addItem(widget);
    
    P J 2 Replies Last reply 20 Mar 2024, 13:20
    0
    • S StudentScripter
      20 Mar 2024, 07:32

      Can't find much information on how to achieve that, so any hint is appreciated. :D
      My goal is to have all items in a layout with adjustable margins between the items. The graphicsitems themself should still be scalable in height if the layout is horizontal and scalable in width if the layout is vertical.

              // Erstellen Sie ein QGraphicsWidget
              QGraphicsWidget *widget = new QGraphicsWidget();
              widget->setFlag(QGraphicsItem::ItemIsMovable);
              QGraphicsWidget *item1 = new QGraphicsWidget;
              item1->setAutoFillBackground(true);
      
              QGraphicsWidget *item2 = new QGraphicsWidget;
              item2->setAutoFillBackground(true);
              // Erstellen Sie ein Layout
              
              QGraphicsRectItem *RectItem = new QGraphicsRectItem();
              QGraphicsLinearLayout *layout = new QGraphicsLinearLayout();
              
              
      
      
              layout->addItem(item1);
              layout->addItem(RectItem);
              layout->addItem(item2);
      
                layout->setSpacing(50);
               layout->setOrientation(Qt::Vertical);
              // Setzen Sie das Layout für das Widget
              widget->setLayout(layout);
      
              // Fügen Sie das Widget zur Szene hinzu
              addItem(widget);
      
      P Offline
      P Offline
      Pl45m4
      wrote on 20 Mar 2024, 13:20 last edited by Pl45m4
      #2

      @StudentScripter

      Read what's written here

      • https://doc.qt.io/qt-6/qgraphicslinearlayout.html#qgraphicslinearlayout-compared-to-other-layouts

      QGraphicsLayout is for QGraphicsWidgets, not QGraphicsItems.


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

      ~E. W. Dijkstra

      S 1 Reply Last reply 20 Mar 2024, 16:27
      2
      • S StudentScripter
        20 Mar 2024, 07:32

        Can't find much information on how to achieve that, so any hint is appreciated. :D
        My goal is to have all items in a layout with adjustable margins between the items. The graphicsitems themself should still be scalable in height if the layout is horizontal and scalable in width if the layout is vertical.

                // Erstellen Sie ein QGraphicsWidget
                QGraphicsWidget *widget = new QGraphicsWidget();
                widget->setFlag(QGraphicsItem::ItemIsMovable);
                QGraphicsWidget *item1 = new QGraphicsWidget;
                item1->setAutoFillBackground(true);
        
                QGraphicsWidget *item2 = new QGraphicsWidget;
                item2->setAutoFillBackground(true);
                // Erstellen Sie ein Layout
                
                QGraphicsRectItem *RectItem = new QGraphicsRectItem();
                QGraphicsLinearLayout *layout = new QGraphicsLinearLayout();
                
                
        
        
                layout->addItem(item1);
                layout->addItem(RectItem);
                layout->addItem(item2);
        
                  layout->setSpacing(50);
                 layout->setOrientation(Qt::Vertical);
                // Setzen Sie das Layout für das Widget
                widget->setLayout(layout);
        
                // Fügen Sie das Widget zur Szene hinzu
                addItem(widget);
        
        J Offline
        J Offline
        JonB
        wrote on 20 Mar 2024, 13:26 last edited by JonB
        #3

        @StudentScripter
        As @Pl45m4 has written. Which means I don't understand how you got the following through the compiler:

        QGraphicsLinearLayout *layout = new QGraphicsLinearLayout();
        QGraphicsRectItem *RectItem = new QGraphicsRectItem();
        layout->addItem(RectItem);
        

        ? If this generated a compiler error you would surely have said so, no?

        1 Reply Last reply
        0
        • P Pl45m4
          20 Mar 2024, 13:20

          @StudentScripter

          Read what's written here

          • https://doc.qt.io/qt-6/qgraphicslinearlayout.html#qgraphicslinearlayout-compared-to-other-layouts

          QGraphicsLayout is for QGraphicsWidgets, not QGraphicsItems.

          S Offline
          S Offline
          StudentScripter
          wrote on 20 Mar 2024, 16:27 last edited by
          #4

          @Pl45m4 But isn't there anyway to create layouts for graphicsitems?

          P 1 Reply Last reply 20 Mar 2024, 18:39
          0
          • S StudentScripter
            20 Mar 2024, 16:27

            @Pl45m4 But isn't there anyway to create layouts for graphicsitems?

            P Offline
            P Offline
            Pl45m4
            wrote on 20 Mar 2024, 18:39 last edited by Pl45m4
            #5

            @StudentScripter

            This example shows how a use case could look like

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

            But as you can see, the custom items inherit from QGraphicsLayoutItem and QGraphicsItem.
            So to properly use a QGraphicsItem in a layout, you have to inherit QGraphicsLayoutItem or use QGraphicsWidget directly... however I don't know how it fits in your existing logic/structure. It might break something... using layouts and "moving free / free resizable by user" is kinda counterproductive.


            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
            0

            1/5

            20 Mar 2024, 07:32

            • Login

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