C ++ Qt QGraphicsView: remembering scrollbar position after reboot

I have the following problem with scrollbars in a graphical representation. My application accepts a PDF file and creates (in some way) a QImage from it. Then QImage converted to QPixmap , which is used to create a QGraphicsScene , and from QGraphicsScene I create a QGraphicsView . QGraphicsView added to the central widget and displayed.

The code looks something 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 value is updated every time a PDF file is modified. Now the problem is that the thing in the PDF can also be resized, and the scroll bars are mixed up. I want the scrollbars after the upgrade to be in position, so that I will see the same part of the PDF file as before the upgrade.

Can you give me some tips on how to do this? I searched for this problem, but so far nothing has worked in my case (maybe I'm wrong too).

Thank you for your responses!

+4
source share
2 answers

Before changing the contents of a view, remember the position of the scroll bar with view->horizontalScrollBar()->value() and view->verticalScrollBar()->value() .

After changing the reset of previous values ​​with setValue .

It’s bad that you did not provide any code that you tried to use for this, so I can’t tell you what you are 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()->setVealue(pos_x); view->verticalScrollBar()->setValue(pos_y); 

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

+2
source

I changed the code, but it behaves strangely.

 QImage image; image = loadImage(path); QPixmap pixmap; pixmap.convertFromImage(image); scene = new QGraphicsScene(this); scene->addPixmap(pixmap); int hsbv = -1; int vsbv = -1; if (view != NULL) { hsbv = view->horizontalScrollBar()->value(); vsbv = view->verticalScrollBar()->value(); } 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); view->horizontalScrollBar()->setVealue(hsbv); view->verticalScrollBar()->setValue(vsbv); 

When I print view->horizontalScrollBar()->value() at the end, I always get 83, and I get 479 for view->verticalScrollBar()->value() , which is very strange, but when I print hsbv and vsbv I get reasonable numbers.

0
source

Source: https://habr.com/ru/post/1488933/


All Articles