PDA

View Full Version : Pan/Move QPixmap in QLabel



ashtray4241
12th August 2014, 09:03
Hi,

First of all, I have seen similar questions in this forum, but I didn't find the answers I was looking for (or maybe I didn't understand the solution).
Ok, now to the question -
I have a QLabel, whose size and location (coordinates) are fixed, and I need to display a image in it. This image can be zoomed/width fitted/page fitted/original view, and if the image is larger than the QLabel, I need to give options to move the image on press of some up/down/left/right QButtons. (exact scenario is that after a Scan, preview image is displayed on our panel, and that needs to be zoomed/panned)

I have been able to do the zoom stuff by using scaled/scaledToWidth/scaledToHeight, but I am having problems in moving/panning the image (QPixmap).

This is my code for down button



mut->lock(); //mutex, just for some test
QRegion exposed;
const QPixmap *p1=label->pixmap(); //label is the QLabel object. This is more of a hit and trial code, and this part was giving some compilation issue with constructor, so the code is written like this
QPixmap *p=new QPixmap(*p1);
p->scroll(0,-10,label->rect(),&exposed);
label->setPixmap(*p);
mut->unlock();

But the problems I am getting are that
1) when I press buttons fast, there is some sort of banding (part of image repeating on one end).
2) I cannot specify any offset using the scroll function, as the image is scrolled only in the QPixmap rect area (not the QLabel area). So the image is not actually moved to the offset. I have attached screenshot for the same

10552

Can someone let me know a better way to do this image panning? (My base QLabel cannot be moved or resized, as that is the preview area)

Thanks in advance
Ash

wysota
12th August 2014, 13:07
Why does it have to be QLabel and not e.g. QGraphicsView? BTW. What is the mutex for? Why so many memory leaks?

ashtray4241
12th August 2014, 18:12
Why does it have to be QLabel and not e.g. QGraphicsView? BTW. What is the mutex for? Why so many memory leaks?

This was just a hit and trial code. Actually in my working environment (in fedora), I can't run QT Creator for some reasons. So, before writing the code there, I have a random project in QT Creator in Windows where I first try a logic out, and once I get the result, I move the code to linux environment in my project.
Mutex was just a trial code, as I mentioned that if I pan the image fast then image gets weird with some banding. (as shown in the image above).
While writign the actual code I will delete the pointers created.

Regarding QGraphicsView, till now what I have seen in that is QGraphicsView has a solid white background (didn't try it out if transparent widget can be created or not). Basically, the above zoom/pan requirement for me is only in the preview cases. In other cases, I just want to display an image on the panel without any border, and the image can be non-rectangular itself (for example, the image of printer which has the 2D shape of a printer.). So I was worried that if I use QGraphicsView, a white background will be seen below the image.

wysota
13th August 2014, 09:18
Maybe you should state the general idea of what you are doing? Maybe your whole app should be implemented in Graphics View? The background can be changed, that is not a problem, the real problem you are going to have anyway is that the top-most widget will handle all mouse input so no events will go to underlying widgets if you want to have any.

ashtray4241
13th August 2014, 14:13
Maybe you should state the general idea of what you are doing? Maybe your whole app should be implemented in Graphics View? The background can be changed, that is not a problem, the real problem you are going to have anyway is that the top-most widget will handle all mouse input so no events will go to underlying widgets if you want to have any.

Thanks for your inputs. I am right now trying the code with Graphics view.
We are in initial phase of our design, and right now we are concentrating on displaying the screens. I guess event handling will work fine as in my sample app, even when a QLabel was on top of QPushButton, the slot for clicked() of QPushButton was called when we clicked it. In any case, if later on I face such issues, I will come back here again for that.

We are basically designing a LCD panel display of a large MFP (Printer like those Xerox machines). Earlier we were using PEG, but since PEG is quite limited, we are moving to QT and so we are tasked with the porting.

Edit: If I have forgotten to say it yet wysota, I really appreciate your replies. They are quick and makes me do some analytical thinking.

faldzip
14th August 2014, 09:50
We are basically designing a LCD panel display of a large MFP (Printer like those Xerox machines). Earlier we were using PEG, but since PEG is quite limited, we are moving to QT and so we are tasked with the porting.
Do you mean something like this: http://wirthconsultingb4youbuy.files.wordpress.com/2013/02/workcentre-5855_5845-touchscreen-jp.jpg
I don't know your exact requirements but I would consider:
1. QGraphicsView for this task if the whole project idea suits better to widgets then to QtQuick
2. QtQuick if you want some nice UI like on the screenshot I've pasted. If your device is OpenGL capable, then you can use QtQuick 2 (which is available in Qt >= 5.0) with QtQuick.Controls and stunning animations and so on :) if there is no OpenGL then you can still use QtQuick 1 (which is no OpenGL accelerated so you should check the performance, but if you do not want any fancy animations, than it should work fine)