I am trying to move around the viewport displayed by the GraphicsView. According to Qt documentation, this is can be done by calling the translate() method of the QGraphicsView class.
I have tried to do this without success so far and I haven't found an explanation to this problem so far. Can anyone help me ? I created a simple example that shows my problem :
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene *scene = new QGraphicsScene();
ui->graphicsView->setScene(scene);
ui->graphicsView->scene()->addItem(new QGraphicsLineItem(0,-10, 0,10));
ui->graphicsView->scene()->addItem(new QGraphicsLineItem(-10,0, 10,0));
}
MainWindow::~MainWindow()
{
delete ui;
}
graphicsView is of a subclassed type of QGraphicsView. Here is its code :
MyGraphicsView::MyGraphicsView(QWidget *parent)
: QGraphicsView(parent)
{
}
void MyGraphicsView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Up)
translate(0, 10);
else if (event->key() == Qt::Key_Down)
translate(0, -10);
else if (event->key() == Qt::Key_Left)
translate(-10, 0);
else if (event->key() == Qt::Key_Right)
translate(10, 0);
update();
}
As the class is very small, I don't think there is a need for sharing the headers. However, if you feel I should do so, just post a comment and I will share them.