PDA

View Full Version : Image split, rearrange. QLabel best way to display?



fire
11th August 2008, 15:49
I have seen another post where someone wanted to split an image into 4 pieces, rearrange them and put back into display.

I am interested in taking an image, removing the bottom pixel line, and moving it to the top of the image. I would like to have this in a loop so the picture is rotating from bottom to top continually (basically making an image marquee).

I have had success in using QLabel to display the picture, using third party code to copy the bottom to top, save the file, then re-read the file into QLabel. However the process is slow and I am looking for a faster alternative.

I thought I could use QImage::copy() but I don't understand how I can rearrange the copied portions and save as a new QImage for display.

jpn
11th August 2008, 16:11
Construct a target pixmap/image with appropriate size, open a painter on it and use QPainter::drawPixmap() or drawImage() with appropriate source and target rects.

fire
11th August 2008, 20:23
Thanks.

Is there a way to create an image from two images? Or an image from a painter?

jpn
11th August 2008, 20:28
Is there a way to create an image from two images?
The same approach works. Construct an image and draw other images on it.


Or an image from a painter?
What do you mean?

fire
11th August 2008, 21:00
I don't see a public function of QImage which is similar to drawImage (being able to direct the data to a specific part of the array).



After drawing two images to my QPainter object, can I convert the QPainter back into a QImage?

jpn
11th August 2008, 21:04
I don't see a public function of QImage which is similar to drawImage (being able to direct the data to a specific part of the array).



After drawing two images to my QPainter object, can I convert the QPainter back into a QImage?
The whole point is to use yet another image as a paint device. In other words, open the painter on a target image. Then draw two images with QPainter and this way you'll have an image constructed from two images in the end.

fire
11th August 2008, 22:40
It is just not clicking for me... I don't understand how to do a paint event inside of a paint event. I have a timer set up so when it fires the bottom 10 lines are added to the top of the image, the rest of the image is moved down. I want "full_img" to be a QImage holding the last images painted. My problem is understanding how to have 2 paintEvents trigger off the timer. The problem is commented in the paintEvent method.
Here is my code, three files:


// 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 “header.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 “header.h”

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

fire
13th August 2008, 18:27
Any suggestions on how to open a Qpainter on a QImage inside a paintEvent?