为什么我无法放大通过 QSizeGrip 添加到 QGraphicScene 的小部件?
Why I can't enlarge a widget added to QGraphicScene by QSizeGrip?
我已经通过 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));});
} }
我无法将小部件放大到图片中显示的那样。
您的 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);
}
});
请注意,虽然此代码有效,但非常麻烦。但是,它可以让您了解如何开始。
我已经通过 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));});
} }
我无法将小部件放大到图片中显示的那样。
您的 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);
}
});
请注意,虽然此代码有效,但非常麻烦。但是,它可以让您了解如何开始。