在QGraphicsScene中的特定坐标中放置和图像

时间:2013-04-23 22:19:41

标签: c++ qt qgraphicsview qgraphicsscene

在下面的代码中,我使用item.setPos(0,0);green.png放在应用程序的左上角(实际上是QGraphicsView)。但是从两个方向出现在中心 - 垂直和水平。请帮助将图像放在0,0坐标中。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QIcon>



int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QGraphicsScene scene;
    scene.setBackgroundBrush(QBrush(QColor(255, 255, 255), Qt::SolidPattern));


    QGraphicsPixmapItem item(QPixmap(":/images/green.png"));
    item.setPos(0,0);
    scene.addItem(&item);


    QGraphicsView view(&scene);
    view.setFixedSize(400, 400);
    view.setWindowTitle("Diamond Dash");
    view.setWindowIcon(QIcon(":/images/red.png"));
    view.show();

    return app.exec();
}

1 个答案:

答案 0 :(得分:0)

我添加了

scene.setSceneRect(0,0, 400, 400);

它有效!