PDA

View Full Version : initializing QPainter with a QImage



Aayush
17th July 2011, 03:44
QPainter painter(&image);

This is a line from one of the examples (Scribble) that comes with QT 4 Package.
I tried to do the same in my app.

QPainter painter(currentFrame->imgQ);
(here currentFrame is an object of a class that contains the QImage imgQ)

Now, when I do this, the error states that there is no any function that matches for my call.

Can you please tell me how to initiate a QPainter with a QImage??
Just so you know, my app is a webcam app and I want to add a feature where u can draw onto an image.

Thanks already!!

mvuori
17th July 2011, 09:16
Instead of trying things with trial and error, you should look into Qt's documentation for how things are suppused to work. In this case QPainter's class reference http://doc.qt.nokia.com/latest/qpainter.html states that it is contructed with a _pointer_ to a QPaintDevice, which a QImage is. Most likely the compiler's error message already told you all this.

Aayush
18th July 2011, 02:56
Thanks... and sorry for looking like an ass here!! hehe

I solved the problem. turns out it was a silly mistake.... I did not provide the QImage's address there (stupid me!!)

anyways.... now I'v come across another problem.... Can you look at my paintEvent() function::

void myApp::paintEvent(QPaintEvent *event){
QPainter painter(imageLabel);
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, currentFrame->imgQ, dirtyRect);

}

here, imageLabel is a QLabel inside myApp (which is a QMainWindow) . I have to paint a modified part of "currentFrame->imgQ" onto the imageLabel. when compiled, i get the error::

QPainter::begin: Paint device returned engine == 0, type: 1

Thanks again!!

Added after 7 minutes:

Okay.. I just had a lightbulb lit on my head. Is it because the paintEvent() doesnt belong to the QLabel?? If yes, how can i write a paintEvent() for this one imageLabel in particular!! (I dont want the paint Event to respond for other QLabels inside my app).....

mahdhaoui
20th October 2011, 12:47
Hello, i'am novice in QT. i try to add ScrollArea to the Scribble example of QT. Do you have idea how i can do that please.
In ScribbleArea constructor i created imageLabel = new QLabel(this); and scrollArea = new QScrollArea(this); with scrollArea->setWidget(imageLabel);
but I can display the scroll Area. Thank you for you cooperation