为什么我不能通过QSizeGrip放大添加到QGraphicScene的小部件?

问题描述 投票:2回答:1

我通过QGraphicsProxyWidget将一个小部件添加到图形场景QGraphicScene中。为了移动它,我将QGraphicsRectitem设置为其父级。使用sizegrip调整窗口小部件的大小。

我第一次创建一个对象时,可以将它放大到某个维度。我第二次放大它比第一次少。第三次少于第二次等等。

在我看来,它表现得随意。为什么会这样?

这是代码:

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

我不能放大窗口小部件,图像中显示的内容。

c++ qt qgraphicsview qgraphicsscene qgraphicsrectitem
1个回答
1
投票

您的拨号无法通过GraphicView的右侧(水平)和底部(垂直)边缘调整大小。如果你让场景足够大,比如2000x2000(setSceneRect(2000, 2000);),会出现滚动条。如果手动移动滚动条,则可以更多地放大小部件。

您还可以通过更改lambda函数来尝试自动滚动条移动,如下所示:

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);
    }
});

请注意,虽然这段代码有效,但是非常麻烦。但是,它可以让你知道如何开始。

© www.soinside.com 2019 - 2024. All rights reserved.