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:
- In a subcalss of QDial, say Dial, add a new signal
void sizeChanged();
- Reimplement the
resizeEvent
of Dial like this:
in dial.cpp
void Dial::resizeEvent(QResizeEvent *event)
{
QDial::resizeEvent(event);
sizeChanged();
}
- Change
auto *dial= new QDial();
to auto *dial= new Dial();
- 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:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…