Search code examples
c++qt4qgraphicsviewqpixmap

How do I move a QPixMapImage around a qgraphicsview


I can add a QPixMapImage to a QGraphicsScene, but then I have two issues.

First I cannot create a pointer to my QPixmapItem declared in the header, where i can with QGraphicsScene.

I get an error "error: no matching function for call to 'QGraphicsPixmapItem::QGraphicsPixmapItem(MainWindow* const)" ... When I create my QGraphicsScene in Main the same way?

Second issue: I cannot move the QGraphicsPixmapItem around when the slider is moved (obv i won't until the pointer is working). But can I even move it, or do i have to repaint it?

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtGui>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QPixmap>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    QPixmap arrow = QPixmap::fromImage(QImage("ARROW.png"));
    arrowItem = new QGraphicsPixmapItem(this);
    arrowItem->setPixmap(arrow);
    ui->graphicsView->setScene(scene);
    ui->horizontalSlider->setMaximum(2000);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_horizontalSlider_sliderMoved(int position)
{
    arrowItem->setPos(position,position);
    scene->addItem(arrowItem);

}

Header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsPixmapItem>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QGraphicsPixmapItem *arrowItem;
    QGraphicsScene *scene;

private slots:
    void on_horizontalSlider_sliderMoved(int position);
};

#endif // MAINWINDOW_H

Solution

  • Issue #1:

    There error you're getting means there is no such constructor declared for QGraphicsPixmapItem; you use either one of these:

    QGraphicsPixmapItem(QGraphicsItem *parent = 0
    #ifndef Q_QDOC
                            // ### obsolete argument
                            , QGraphicsScene *scene = 0
    #endif
            );
        QGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = 0
    #ifndef Q_QDOC
                            // ### obsolete argument
                            , QGraphicsScene *scene = 0
    #endif
            );
    

    Issue#2

    If I understood your question correctly you want to be able to drag your graphics items around the scene. You can switch this functionality on by using view->setDragMode method.

    Please see if an example below would work for you:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QGraphicsScene* scene = new QGraphicsScene(QRect(-50, -50, 400, 200));
    
        QPixmap image = QPixmap::fromImage(QImage("image.JPG"));
        QGraphicsPixmapItem* pixMapItem = new QGraphicsPixmapItem();
        pixMapItem->setPixmap(image);
        scene->addItem(pixMapItem);
    
        QGraphicsView* view = new QGraphicsView(ui->centralWidget);
        view->setScene(scene);
        view->setGeometry(QRect(50, 50, 400, 200));
        view->setDragMode(QGraphicsView::ScrollHandDrag);
        view->show();
    }
    

    hope this helps, regards