2

I have added a widget to a graphic scene QGraphicScene through a QGraphicsProxyWidget. To move it I have set QGraphicsRectitem as its parent. The widget is resized with the use of a sizegrip.

The first time I create an object I can enlarge it upto some dimension. The second time I can enlarge it less than the first one. The third time less than the second one and so on.

It seems to me that it behaves randomly. Why is this happening?

Here is the code:

void GraphicsView::dropEvent(QDropEvent *event)// subclass of QGraphicsView
{

  if(event->mimeData()->text() == "Dial")
  {
   auto *dial= new Dial;      // The widget
   auto *handle = new QGraphicsRectItem(QRect(event->pos().x(),event->pos().y(), 120, 120));    // Created to move and select on scene
   auto *proxy = new QGraphicsProxyWidget(handle); // Adding the widget through the proxy
   dial->setGeometry(event->pos().x()+10,event->pos().y()+10, 100, 100);
   proxy->setWidget(dial);
   QSizeGrip * sizeGrip = new QSizeGrip(dial);
   QHBoxLayout *layout = new QHBoxLayout(dial);
   layout->setContentsMargins(0, 0, 0, 0);
   layout->addWidget(sizeGrip, 0, Qt::AlignRight | Qt::AlignBottom);

   handle->setPen(QPen(Qt::transparent));
   handle->setBrush(Qt::gray);
   handle->setFlags(QGraphicsItem::ItemIsMovable |
   QGraphicsItem::ItemIsSelectable);

   scene->addItem(handle); // adding to scene 

   connect(dial, &Dial::sizeChanged, [dial, handle](){ handle->setRect(dial->geometry().adjusted(-10, -10, 10, 10));}); 
  }  }  

code

I cannot enlarge the widget more than that, what is shown in the image.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Sagar A W
  • 338
  • 1
  • 12
  • Your code creates one Dial. Show, please, where and how you create the other dials. I already suspect what is the cause, but show it please, just to be sure. – scopchanov Aug 28 '18 at 15:16
  • @scopchanov Dynamically creating objects.. When a dial object dropped on scene it creates dial added to scene...void GraphicsScene::dropEvent(QDropEvent *event) from this event. – Sagar A W Aug 29 '18 at 04:38
  • Great, but could you please show the exact code for the creation? It is important for what I guess is the problem. – scopchanov Aug 29 '18 at 09:23
  • @scopchanov i added verficable example link Please go throgh this https://stackoverflow.com/questions/52069804/in-qgraphicscene-why-enlarging-widget-restricted-after-some-size?noredirect=1#comment91094055_52069804 – Sagar A W Aug 29 '18 at 09:49
  • @scopchanov I am creating objects dynamically when object item is dropped on graphic scene... – Sagar A W Aug 29 '18 at 09:54
  • I understand that, but unfortunatelly that does not give me the information I am looking for. I really want to help you with your problem, but in order to do that I need to see the exact code for that. Show please your drop event handler. – scopchanov Aug 29 '18 at 09:57
  • 1
    @scopchanov drop event added...In Drop event based on drop text creating objects . Graphics View is subclass of QGraphicsView... To resize,move and select widget in scene any other solutions are welcome... – Sagar A W Aug 29 '18 at 10:07
  • 1
    @Sagar Why do you create 2 post with the same question? What do you expect to happen? Please delete the other post, it bothers to see 2 posts with the same content. – eyllanesc Aug 29 '18 at 10:09
  • @Sagar, _second time i can enlarge less than first one_ is not true. Try to move the widget upwards and to the left and you will be able to resize it as much as the first one. – scopchanov Aug 29 '18 at 12:12
  • @scopchanov yes you are correct but first one also prob.. can't enlarge more than that...it's restricted that size only. – Sagar A W Aug 29 '18 at 12:44
  • @Sagar, in the constructor of _GraphicsView_, after `scene` is set as the scene of the view, add `setSceneRect(0, 0, 2000, 2000);` and you will probably see two scrollbars. Resize the widget, then move the scrollbars and you can resize it more. – scopchanov Aug 29 '18 at 13:06
  • @scopchanov real project pic link... Here i can move that object and resize but its restricated to some size ..https://drive.google.com/open?id=18KbT87QfmsQBanM0TgUKrhGKb_VsT_-n – Sagar A W Aug 29 '18 at 13:50
  • @Sagar, what happens if you make the window bigger? – scopchanov Aug 29 '18 at 13:57
  • @scopchanov i setted scene rect to (2000,2000) but same problem i am getting. – Sagar A W Aug 29 '18 at 14:08
  • @Sagar, please answer this: _what happens if you make the window bigger?_ – scopchanov Aug 29 '18 at 14:13
  • @scopchanov sorry i am not getting which window...maximized window only is their – Sagar A W Aug 29 '18 at 14:28
  • @Sagar, the application window. Do you have scrollbars when you make the scene 2000x2000? – scopchanov Aug 29 '18 at 14:30
  • @scopchanov yes you are correct... scroll Bar moving i can resize....may be scene rect problem – Sagar A W Aug 29 '18 at 14:36
  • @Sagar, try to adjust the position of the scrollbars when resizing the widget. – scopchanov Aug 29 '18 at 14:47
  • @scopchanov scene rect problem...In running project initially scene is small , i can enlarge upto some size, if i enlarge scene means again i can enlarge that object upto some more size... – Sagar A W Aug 29 '18 at 14:49
  • @scopchanov Thanq for your patienance.. yes i can resize after scrolling – Sagar A W Aug 29 '18 at 14:50
  • @Sagar, you are welcome! – scopchanov Aug 29 '18 at 14:51
  • @scopchanov yes... – Sagar A W Aug 30 '18 at 03:26

1 Answers1

1

Your Dial can't be resized past the GraphicView's right (horizonally) and bottom (vertically) edges. If you make the scene big enough, say 2000x2000 (setSceneRect(2000, 2000);), scrollbars will appear. If you move the scrollbars manually, you will be able to enlarge your widgets more.

You could also experiment with automatic scrollbar movement by changing the lambda function like this:

connect(dial, &Dial::sizeChanged, [this, dial, handle](){
    handle->setRect(dial->geometry().adjusted(-10, -10, 10, 10));

    int dx = handle->rect().bottomRight().x() > viewport()->rect().bottomRight().x();
    int dy = handle->rect().bottomRight().y() > viewport()->rect().bottomRight().y();

    if (dx > 0) {
        horizontalScrollBar()->setValue(horizontalScrollBar()->value() + dx);
    }

    if (dy > 0) {
        verticalScrollBar()->setValue(verticalScrollBar()->value() + dy);
    }
});

Please note, that although this code works, is is very cumbersome. However, it could give you an idea how to start.

scopchanov
  • 7,966
  • 10
  • 40
  • 68