Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我不能放大一个通过QSizeGrip添加到Qgraphicsene的小部件?_C++_Qt_Qgraphicsview_Qgraphicsscene_Qgraphicsrectitem - Fatal编程技术网

C++ 为什么我不能放大一个通过QSizeGrip添加到Qgraphicsene的小部件?

C++ 为什么我不能放大一个通过QSizeGrip添加到Qgraphicsene的小部件?,c++,qt,qgraphicsview,qgraphicsscene,qgraphicsrectitem,C++,Qt,Qgraphicsview,Qgraphicsscene,Qgraphicsrectitem,我通过QGraphicsProxy小部件向图形场景QGraphicsScene添加了一个小部件。要移动它,我已将QGraphicsRectitem设置为其父项。使用sizegrip调整小部件的大小 第一次创建对象时,我可以将其放大到某个维度。第二次我可以把它放大得比第一次小。第三次少于第二次,以此类推 在我看来,它的行为是随机的。为什么会这样 代码如下: void GraphicsView::dropEvent(QDropEvent *event)// subclass of QGraphics

我通过QGraphicsProxy小部件向图形场景QGraphicsScene添加了一个小部件。要移动它,我已将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));}); 
  }  }  

我无法放大图像中所示的小部件。

您的拨号盘无法调整到超出GraphicView右水平和垂直底部边缘的大小。如果将场景设置得足够大,请使用2000x2000 setscenerect20002000;,将出现滚动条。如果您手动移动滚动条,您将能够进一步放大小部件

您还可以通过如下更改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);
    }
});

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

您的代码创建一个拨号盘。请显示您创建其他拨号盘的位置和方式。我已经怀疑是什么原因造成的,但请出示它,只是为了确定。@scopchanov动态创建对象。。当拨号对象放置在场景中时,它会创建添加到场景中的拨号…void graphicscene::dropEventQDropEvent*事件。很好,但您能否显示创建的确切代码?这对于我猜想的问题很重要。@scopchanov drop event添加了…在基于drop text创建对象的drop event中。图形视图是QGraphicsView的子类。。。若要在场景中调整、移动和选择小部件,欢迎使用任何其他解决方案…@Sagar为什么创建两篇带有相同问题的帖子?你预计会发生什么?请删除另一篇文章,它很麻烦看到两篇内容相同的文章。@Sagar,很抱歉,我不明白。实际上,现在通过sizegrip调整拨号对象的大小,然后设置QGraphics rect项的rect,执行相反操作,调整graphics rect项的大小,然后设置dial@Sagar,我现在明白了。因此我的问题是:你打算如何调整QGraphicsRectitem的大小?我得到了这个链接的代码。。。我可以调整qgraphicsrect的大小。。但是拨对象icant@Sagar,似乎是时候问另一个问题了: