PDA

View Full Version : drawing on a QPixmap or QImage



gren15
25th February 2009, 19:59
Can you draw on a QPixmap/ QImage?

I tried the following but it doesn't work:



QPixmap pixmap;
if ( pixmap.load("myImage.png") ) // load and draw image
p->drawPixmap( 100, 85, pixmap );
QPainter pixPaint(pixmap); // Error: Can't convert from QPixmap to const QPaintDevice . But doesn't QPixmap inherit QPaintDevice???
pixPaint.drawLine(1,5);

wysota
25th February 2009, 23:01
Pass a pointer to the pixmap.

spirit
26th February 2009, 06:24
i.e.


...
QPainter pixPaint(&pixmap);
...

;)

qtUser500
26th February 2009, 15:43
Try adding a brush like


QPainter pixPaint(&pixmap);
QBrush brush2( Qt::blue);
pixPaint.setBrush(brush2);
pixPaint.drawLine(1,5);

Hope that helps!

gren15
26th February 2009, 16:07
i.e.


...
QPainter pixPaint(&pixmap);
...

;)

Thanks for all your inputs.

I tried added a brush/ pen to QPainter, but I still don't see the line.:confused:

wysota
26th February 2009, 21:15
What is the current code?

gren15
26th February 2009, 21:35
I tried the code


QPainter pixPaint(&pixmap);
QPen myPen( Qt::green);
pixPaint.setPen(myPen);
pixPaint.drawLine(5,5, 10,10);

wysota
26th February 2009, 21:42
This seems to work quite fine for me:

#include <QApplication>
#include <QPixmap>
#include <QPainter>
#include <QLabel>

int main(int argc, char **argv){
QApplication app(argc, argv);
QPixmap px;
px.load(argv[1]);
QPainter p(&px);
p.setPen(Qt::blue);
p.drawLine(5,5, 40, 40);
p.end();
QLabel label;
label.setPixmap(px);
label.show();
return app.exec();
}

gren15
27th February 2009, 14:56
I am not able to compile, get an error C2512: 'QLabel' : no appropriate default constructor available. Tried using

QLabel *label = new QLabel(QWidget*); // but this needs a QWidget

1. Does a pixmap always need a QLabel, for it to be set to?
2. I tried on a different widget like canvas and listbox, here I see the pixmap, but not the line drawn over the pixmap.:confused:

aamer4yu
27th February 2009, 17:57
I am not able to compile, get an error C2512: 'QLabel' : no appropriate default constructor available.
Did you compile the code wysota gave ?? if changed, what did you write ?

1. Does a pixmap always need a QLabel to be set to?
Nope, pixmap is a paintdevice, and can be rendered onto any widget. using QLabel for showing pixmap is the simplest way since you dont need to DRAW the pixmap onto the painter, but simply set a pixmap for the label.

2. I tried on a canvas, where I can get to see the pixmap, but not the line drawn over the pixmap.
Can we see what you tried ?

gren15
27th February 2009, 21:01
The eventual direction I want to take is to create a listbox with pictures of animals. I want to select a few pictures (Eg grassland animals) and surround them with a green rectangle.

This is what I tried:


QPixmap pxWolf ("wolf.jpg");
QPainter *animalPainter = new QPainter(&pxWolf );

QListBoxPixmap *animalItem = new QListBoxPixmap(animalListBox, pxWolf, "wolf"); // displays fine, I see the pixmap and the text

// I want to border this animal with a green line
QPainter *borderPainter = new QPainter(animalItem);
borderPainter->setPen(Qt::green);
borderPainter->begin(&pxWolf);
borderPainter->drawRect(QRect(1,1, 10,10)); // I don't get to see this green rectangle
borderPainter->end();

I will be glad for any help on this. Thanks

wysota
27th February 2009, 22:12
Is the question related to Qt3?

gren15
1st March 2009, 04:00
Opps! Forgot to mention it .. yes it is using Qt3.

wysota
1st March 2009, 08:57
Can you prepare a minimal compilable example reproducing the problem?

gren15
2nd March 2009, 13:56
Tweaking the QT listbox example, I have enclosed an executable version of what I want to do.

Quick question: To resize the pixmap to a different size from the default size, I tried the resize function but it cuts the image, not resize it. I have also added this in the enclosed file.


pixmap.resize(50,80);

gren15
3rd March 2009, 17:48
Thanks everyone. I was able to resolve it.:D