Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Qt QGraphicsView: Remembering the position of scrollbars after reload

Tags:

c++

qt

I have the following problem with scrollbars in graphics view. My application takes a PDF file and creates (in some way) a QImage out of it. The QImage is then converted to QPixmap, which is used to create a QGraphicsScene and from the QGraphicsScene I create a QGraphicsView. The QGraphicsView is added to the central widget and displayed.

The code looks approximately like this

QImage image;
image = loadImage(path);

QPixmap pixmap;
pixmap.convertFromImage(image);

scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);

view = new QGraphicsView(scene);

textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);

In the application, the view gets updated every time the PDF file changes. Now the problem is that the thing, which is in the PDF file, can also change in size and the scrollbars get messed up. I want the scrollbars after update to be in a position, such that I will see the same part of the PDF file as I saw before the update.

Can you give me some advice on how to accomplish this? I've searched for this issue, but nothing has worked in my case so far (I could have also be doing something wrong).

Thank you for your answers!

like image 869
pizet Avatar asked Jun 30 '13 08:06

pizet


1 Answers

Before changing the view's contents remember scrollbar positions using view->horizontalScrollBar()->value() and view->verticalScrollBar()->value().

After changing reset previous values using setValue(int).

It's bad that you didn't provide any code that you have tried to use to accomplish this so I can't tell you what were you doing wrong.

Here is an example:

int pos_x = view->horizontalScrollBar()->value();
int pos_y = view->verticalScrollBar()->value();
pixmap_item->setPixmap(QPixmap::fromImage(new_image));
view->horizontalScrollBar()->setValue(pos_x);
view->verticalScrollBar()->setValue(pos_y);

Here pixmap_item is the stored result of scene->addPixmap(pixmap). It has QGraphicsPixmapItem* type.

like image 178
Pavel Strakhov Avatar answered Nov 02 '22 23:11

Pavel Strakhov