gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:01 30 4
gpt4 key购买 nike

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com