本文介绍了如何调整通过QGraphicsProxy添加在QGraphicScene中的QWidget的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过QGraphicsProxyWidget将小部件添加到图形场景(QGraphicScene).要移动并选择小部件,请添加QGraphicsRectItem句柄.要调整窗口小部件的大小,请将QSizegrip添加到窗口小部件.但是当我调整小部件的大小超过QGraphicsRect项时,rect右和底边就落后了.如何解决这个问题?当我调整窗口小部件图形的大小时,rect项目应调整大小,反之亦然.怎么做?欢迎其他任何想法.这是代码

  auto * dial = new QDial();//小部件自动*句柄=新的QGraphicsRectItem(QRect(0,0,120,120));//创建以在场景中移动和选择自动*代理=新的QGraphicsProxyWidget(handle);//通过代理添加小部件Dial-> setGeometry(0,0,100,100);拨-> move(10,10);proxy-> setWidget(拨号);QSizeGrip * sizeGrip =新的QSizeGrip(拨号);QHBoxLayout * layout =新的QHBoxLayout(拨号);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);//添加到场景 

这是输出::
调整大小之前
调整大小后

解决方案

原因

您用作句柄的 QGraphicsRectItem 无法识别 QDial 的大小变化,因此它不会通过调整自身大小来进行响应.

限制

QWidget 及其子类没有提供类似 sizeChanged 信号之类的东西.

解决方案

考虑原因和给定的限制,我的解决方法是:

  1. Dial 的子cals中,例如 Dial ,添加一个新信号 void sizeChanged();
  2. 像这样重新实现 Dial resizeEvent :

在dial.cpp

  void Dial :: resizeEvent(QResizeEvent * event){QDial :: resizeEvent(event);sizeChanged();} 
  1. auto * dial = new QDial(); 更改为 auto * dial = new Dial();
  2. Scene-> addItem(handle);之后添加以下代码;//添加到场景中:

您的示例代码所在的地方

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

注意:也可以使用

I have added a widget to a graphic scene (QGraphicScene) through a QGraphicsProxyWidget. To move and select the widget added QGraphicsRectItem handle.To resize widget added QSizegrip to widget. But when i resize widget more than the QGraphicsRect item rect right and bottom edges goes behind .How to overcome this problem?When i resize widget graphics rect item should resize or vice-versa should happen. how to do this? Any other ideas are welcome.Here is the code

     auto *dial= new QDial();                                        // The widget
     auto *handle = new QGraphicsRectItem(QRect(0, 0, 120, 120));    // Created to move and select on scene
     auto *proxy = new QGraphicsProxyWidget(handle);                 // Adding the widget through the proxy

     dial->setGeometry(0, 0, 100, 100);
     dial->move(10, 10);

     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 

Here is the Output::
Before Resize
After Resize

解决方案

Cause

The QGraphicsRectItem, which you use as a handle, is not aware of the size changes of QDial, so it does not respond by resizing itself.

Limitation

QWidget and its subclases do not provide something like a sizeChanged signal out of the box.

Solution

Considering the cause and the given limitation, my solution would be the following:

  1. In a subcalss of QDial, say Dial, add a new signal void sizeChanged();
  2. Reimplement the resizeEvent of Dial like this:

in dial.cpp

void Dial::resizeEvent(QResizeEvent *event)
{
    QDial::resizeEvent(event);

    sizeChanged();
}
  1. Change auto *dial= new QDial(); to auto *dial= new Dial();
  2. Add the following code after Scene->addItem(handle); // adding to scene:

in the place, where your example code is

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

Note: This could be also solved using eventFilter instead of subclassing QDial. However, from your other question I know that you already subclass QDial, that is why I find the proposed solution more suitable for you.

Result

This is the result of the proposed solution:

这篇关于如何调整通过QGraphicsProxy添加在QGraphicScene中的QWidget的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:56