c++ - 为什么我无法放大通过 QSizeGrip 添加到 QGraphicScene 的小部件?

标签 c++ qt qgraphicsview qgraphicsscene qgraphicsrectitem

我通过 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

我无法将小部件放大到图片中显示的那样。

最佳答案

您的 Dial 无法调整超过 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);
    }
});

请注意,虽然此代码有效,但非常麻烦。但是,它可以让您了解如何开始。

关于c++ - 为什么我无法放大通过 QSizeGrip 添加到 QGraphicScene 的小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060862/

相关文章:

php - 在 PHP 与 C++ 中处理

基于 UNIX 套接字的客户端程序中的 C++ connect() 不建立与服务器的连接

Python 和 PySide : Variable values reset when calling functions from another file

qt - QGraphicsItem 沿着路径移动

c++ - 对于非空初始化,lifetime starts before initialization 解决了什么问题?

c++ - 如何在 unordered_map 的键中使用 std::tr1::function 对象?

c++ - Qt:在QThread拷贝的子类中,构造函数被编译器删除

python - Python 中的 Mipmap?

python - 无法完全删除 PyQt QGraphicsView 的边框

c++ - 获取 QGraphicsView 的大小