将Dial Object添加到场景中。要移动对象,将QGraphicsRectitem设置为父对象,并通过sizegrip调整窗口小部件的大小。使用lamda函数更改小部件的几何形状并调整其父QGraphicsRectItem的大小。拨号对象之后我无法将其调整为某些大小。为什么发生这种情况?
这是代码
Dial.h
#include <QDial>
class Dial : public QDial
{
Q_OBJECT
public:
Dial(QWidget * parent = nullptr);
signals:
void sizeChanged();
protected:
void resizeEvent(QResizeEvent *event);
};
Dial.cpp
#include "dial.h"
Dial::Dial(QWidget *parent ) :
QDial(parent)
{
}
void Dial::resizeEvent(QResizeEvent *event)
{
QDial::resizeEvent(event);
emit sizeChanged();
}
mainWindow.cpp
QGraphicsView *view = new QGraphicsView();
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QGraphicsScene *scene = new QGraphicsScene();
scene->setBackgroundBrush(QBrush(Qt::cyan, Qt::SolidPattern));
view->setSceneRect(0,0,view->frameSize().width(),view->frameSize().height());
view->setScene(scene);
setCentralWidget(view);
auto *dial= new Dial(); // The widget // Dial subclass of QDial
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(10,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));});