简体   繁体   English

Qt-使用绝对位置将项目添加到QGraphicsScene

[英]Qt - Adding items to QGraphicsScene with absolute position

This is probably a dump question but I'm really stuck with it right now. 这可能是一个转储问题,但我现在真的很坚持。 I'm trying to draw some squares in a QGraphicsScene and I want them to be aligned from position x = 0 towards the positive position of x coordinates. 我正在尝试在QGraphicsScene中绘制一些正方形,并且希望它们从位置x = 0朝x坐标的正位置对齐。 However they are aligned according to the alignment configuration of the QGraphicsView and the setting of position is only effective for the second item and upwards relative to the first item! 但是,它们根据QGraphicsView的对齐配置进行对齐,并且位置设置仅对第二项有效,并且相对于第一项有效! This means that if I have a single item, then setting of it's position has no effect. 这意味着如果我只有一个项目,则设置其位置无效。 Mainly this line seems not to be working: 主要是这一行似乎不起作用:

graphicsView->setAlignment(Qt::AlignAbsolute);

This is my code: 这是我的代码:

QGraphicsScene *scene = new QGraphicsScene();
QGraphicsView *graphicsView;
graphicsView->setScene(scene);
graphicsView->setAlignment(Qt::AlignAbsolute);

for(int i = 0; i < 500; i+= 50)
{
    QGraphicsPolygonItem *item = new QGraphicsPolygonItem();

    item->setPen(QPen(QColor(qrand()%255,qrand()%255,qrand()%255)));
    item->setBrush(QBrush(QColor(255,251,253)));
    item->setPolygon(*myPolygon);
    graphicsView->scene()->addItem(item);
    item->setPos(i , 40);
    item->setFlag(QGraphicsItem::ItemIsMovable, true);
    item->setFlag(QGraphicsItem::ItemIsSelectable, true);
    graphicsView->show();
}

I do not know what the problem might be, so I tried the following code 我不知道可能是什么问题,所以我尝试了以下代码

const QRectF rect = QRectF(0, 0, ui->graphicsView->width(), ui->graphicsView->height());
ui->graphicsView->setScene(&_scene);
ui->graphicsView->fitInView(rect, Qt::KeepAspectRatio);
ui->graphicsView->setSceneRect(rect);

With respect to the previous four lines, the following output does not produce sizes even close to each other: 关于前四行,以下输出不会产生彼此接近的尺寸:

qDebug() << "scene =>" << _scene.width() << _scene.height();
qDebug() << "graphicview =>" << ui->graphicsView->width() << ui->graphicsView->height();

I highly appreciate your help. 非常感谢您的帮助。

It seems that Qt::AlignAbsolute does not do what you assume it does. 看来Qt::AlignAbsolute并没有按照您的假设做。 What you actually need is Qt::AlignLeft | Qt::AlignTop 您真正需要的是Qt::AlignLeft | Qt::AlignTop Qt::AlignLeft | Qt::AlignTop . Qt::AlignLeft | Qt::AlignTop

It is explained here: http://qt-project.org/doc/qt-4.8/qgraphicsview.html#alignment-prop http://qt-project.org/doc/qt-4.8/qt.html#AlignmentFlag-enum 此处说明: http : //qt-project.org/doc/qt-4.8/qgraphicsview.html#alignment-prop http://qt-project.org/doc/qt-4.8/qt.html#AlignmentFlag-枚举

In my code, I only use graphicsView->setSceneRect(rect); 在我的代码中,我只使用graphicsView->setSceneRect(rect); and not fitInView() . 而不是fitInView() The latter introduces a scaling and may hinder your understanding on what is going wrong. 后者会导致缩放,并可能会妨碍您理解问题所在。

Basically, I overwrite the QGraphicsview to re-implement resizeEvent() : 基本上,我覆盖了QGraphicsview以重新实现resizeEvent()

void AutohideView::resizeEvent(QResizeEvent *event)
{
    /* always resize the scene accordingly */
    if (scene())
        scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));

    QGraphicsView::resizeEvent(event);
}

I do not alter alignment or any other options and use absolute coordinates in the range [ 0 , 0 .. scene()->width() , scene()->height() ] when positioning my items. 我不改变取向或任何其他选项,并在范围使用绝对坐标[ 00 .. scene()->width() scene()->height() ]定位我的项目时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM