PDA

View Full Version : Multiple QPainters on QImage



fire
13th August 2008, 22:03
I am having problems with two different objects I can paint to (one for display and one for temporary holding of an image). I can paint to a widget and to an image separately, but not sure how to paint the same image to the widget and to a QImage residing inside the widget. I would like the widget and QImage to be updated during the same trigger event.

When I try this code, I get:
QRasterPaintEnginer::begin: Unsupported image format (3)
QPainter::begin(): Returned false
QPainter::end: Painter not active, aborted

I understand it is from having two QPainters in the paintEvent. I want full_img to be holder of the image, move the bottom 10 rows of the image to the top using drawImage on the painter, then save or paint the image I just painted to the widget to full_img.



// myMI.h
#include <QWidget>
class MarqueeImage : public QWidget
{
Q_OBJECT
public:
MarqueeImage(QWidget *parent = 0);

protected slots:
void paintEvent(QPaintEvent *event);

private:
QImage top_img; // top part of the image minus 10 lines
QImage bot_img; // bottom 10 lines
QImage *full_img; // temporary image
int myheight; // height of image
int mywidth; // width of image
int line_shift; // bottom 10 lines
};

// myMI.cpp
#include “myMI.h”
#include <QPainter>
#include <QTimer>

MarqueeImage::MarqueeImage(QWidget *parent)
: QWidget(parent)
{
mywidth = 200; //bitmap width
myheight = 300; //bitmap height
line_shift = 10; //number of lines being moved from bottom to top

full_img = new QImage();
full_img->load(“mybmp.bmp”);

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(500);
}

void MarqueeImage::paintEvent(QPaintEvent *)
{
QPainter painter(this);
top_img = full_img->copy(0, 0, mywidth-1, myheight-shift-1);
bot_img = full_img->copy(0, myheight-line_shift-1, mywidth-1, shift);
painter.drawImage(0, 0, bot_img);
painter.drawImage(0, shift, top_img);

// This is where it doesn’t work
QPainter paint_all(full_img);
paint_all.drawImage(0, 0, bot_img);
paint_all.drawImage(0, shift, top_img);

}


// main.cpp
#include <QApplication>
#include “myMI.h”

int main(int argc, char *argv[])
{
Qapplication app(argc, argv);
MarqueeImage myMI;
myMI.show();
return app.exec();
}

wysota
14th August 2008, 10:26
Why do you want to repaint the image during every paintEvent? If nothing changed, the image doesn't have to be repainted. Implement a render method for your content that takes a QPainter* as an argument and when you want to repaint the image call it on the image's painter. In the paintEvent call it again but on the widget's painter.

fire
14th August 2008, 13:07
Why do you want to repaint the image during every paintEvent? If nothing changed, the image doesn't have to be repainted. Implement a render method for your content that takes a QPainter* as an argument and when you want to repaint the image call it on the image's painter. In the paintEvent call it again but on the widget's painter.

Although I have it set up as a rotating picture, I plan to change the code to read new pixel data from a file source and place it at the top of the picture.

I would also like to be able to place the updated images to another widget and/or save it to a file, hence why I wanted a place holder (the QImage object) for the new image data.

wysota
14th August 2008, 13:10
But it doesn't answer why you repaint the image in the paintEvent and not elsewhere. The paintEvent may be called for instance when something obscures your widget. There is no point in redrawing the image in such a situation.