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

Why I can#39;t enlarge a widget added to QGraphicScene by QSizeGrip?(为什么我不能放大 QSizeGrip 添加到 QGraphicScene 的小部件?)
本文介绍了为什么我不能放大 QSizeGrip 添加到 QGraphicScene 的小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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?

代码如下:

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

我不能将小部件放大更多,如图所示.

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

推荐答案

您的 Dial 无法调整大小超过 GraphicView 的右侧(水平)和底部(垂直)边缘.如果你让场景足够大,比如 2000x2000 (setSceneRect(2000, 2000);),滚动条就会出现.如果您手动移动滚动条,您将能够进一步放大您的小部件.

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.

您还可以通过像这样更改 lambda 函数来试验自动滚动条移动:

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.

这篇关于为什么我不能放大 QSizeGrip 添加到 QGraphicScene 的小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)