問題描述
我通過 QGraphicsProxyWidget 添加了一個小部件到圖形場景 QGraphicScene.為了移動它,我將 QGraphicsRectitem 設(shè)置為它的父級.使用 sizegrip 調(diào)整小部件的大小.
I have added a widget to a graphic scene QGraphicScene through a QGraphicsProxyWidget. To move it I have set QGraphicsRectitem as its parent. The widget is resized with the use of a sizegrip.
第一次創(chuàng)建對象時,我可以將其放大到某個尺寸.第二次我可以把它放大得比第一次小.第三次小于第二次以此類推.
The first time I create an object I can enlarge it upto some dimension. The second time I can enlarge it less than the first one. The third time less than the second one and so on.
在我看來,它的行為是隨機(jī)的.為什么會發(fā)生這種情況?
It seems to me that it behaves randomly. Why is this happening?
代碼如下:
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));});
} }
我不能將小部件放大更多,如圖所示.
I cannot enlarge the widget more than that, what is shown in the image.
推薦答案
您的 Dial 無法調(diào)整大小超過 GraphicView 的右側(cè)(水平)和底部(垂直)邊緣.如果你讓場景足夠大,比如 2000x2000 (setSceneRect(2000, 2000);
),滾動條就會出現(xiàn).如果您手動移動滾動條,您將能夠進(jìn)一步放大您的小部件.
Your Dial can't be resized past the GraphicView's right (horizonally) and bottom (vertically) edges. If you make the scene big enough, say 2000x2000 (setSceneRect(2000, 2000);
), scrollbars will appear. If you move the scrollbars manually, you will be able to enlarge your widgets more.
您還可以通過像這樣更改 lambda 函數(shù)來試驗自動滾動條移動:
You could also experiment with automatic scrollbar movement by changing the lambda function like this:
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);
}
});
請注意,雖然此代碼有效,但非常繁瑣.但是,它可以讓您了解如何開始.
Please note, that although this code works, is is very cumbersome. However, it could give you an idea how to start.
這篇關(guān)于為什么我不能放大 QSizeGrip 添加到 QGraphicScene 的小部件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!