PDA

View Full Version : Question about painting.



Datakim
5th July 2012, 16:52
Hi, I have the following dilemma.

I have a picture that I need to be able to draw (and also write) on. I currently have managed to make a Qlabel and place the image on the label through pixmap. Ultimately I will need to print the (modified) image to a printer.

So here is the question, is there any way to draw directly on the pixmap thats on the Qlabel, and if so how? I have tried googling but found nothing comprehensible.

Thanks :)

amleto
5th July 2012, 20:35
inherit from qlabel and implement your own paint event, or look into qgraphicsview/scene/item framework

Datakim
5th July 2012, 21:16
Unfortunately I have very little idea how to do those things, but thanks for trying. :)

wysota
5th July 2012, 21:43
Unfortunately I have very little idea how to do those things, but thanks for trying. :)

Hmm... so what exactly you were expecting?

Datakim
5th July 2012, 22:05
Hmm... so what exactly you were expecting?

Basically, I have a jpg image. Lets call it image1.jpg. I have been trying to read information from google, and currently I have an ordinary Qlabel, to which I have managed to put the image1.jpg.

However I need to be able to add text to that image. Basically I get text from a database, and I need to be able to write that text on the picture. The text itself changes depending on what line a user selects from the mysql database.

I am rather inexperienced with QT, so despite trying many different solutions I have found on the net, I just cannot write text to the pixmap image on the label. In all cases I either get an error, nothing happens and in one case the image was just replaced with another blank pixmap.

Ultimately I need to be able to print the (modified) image on paper.

So if I wanted to load an image, and then write text to that (to specific coordinates on the image) how would I go about doing that. If someone could give me a quick example I would be truly gratefull. What would be the best and most simple way to do this: load image, show image, add text from database to specific coordinates in the image and finally print it out.

Thanks :)

wysota
5th July 2012, 22:26
No no... I meant what kind of answer were you expecting to receive? "Use QLabel::doWhatDatakimWants(image)"? Writing your own classes and reimplementing virtual methods is basics of C++, you just have to learn it.

mvuori
5th July 2012, 22:38
Basically, I have a jpg image. Lets call it image1.jpg. I have been trying to read information from google, and currently I have an ordinary Qlabel, to which I have managed to put the image1.jpg.

However I need to be able to add text to that image.
But you don't need to add the text on the QLabel. You can keep the image in memory, add the text and then put it in the QLabel.

1) Load the image file

QImage picture(filename);

...create a QPainter for it:
QPainter painter(&picture);

...Now you can do whatever to the image, before you put that into your label
painter.drawText(...);
(...)

painter.end();

label->setPixmap(QPixmap::fromImage(picture));

Datakim
5th July 2012, 23:12
But you don't need to add the text on the QLabel. You can keep the image in memory, add the text and then put it in the QLabel.

1) Load the image file

QImage picture(filename);

...create a QPainter for it:
QPainter painter(&picture);

...Now you can do whatever to the image, before you put that into your label
painter.drawText(...);
(...)

painter.end();

label->setPixmap(QPixmap::fromImage(picture));

Thank you so much! That was exactly what I needed. :)