I'm combining 2 QImages ("firstImage" and "secondImage") vertically with the help of QPainter. Combined image displays properly, with no error, as seen from the screen shot:
Combined Image Displays Properly
But if I want to store the "combinedImage" by using .save, program halts and I get assert error.
How can I store the combined image into the harddisk?
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QImage>
#include <QPaintEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QString firstImagePath = "/home/nvidia/Desktop/TestFolder/firstImage.jpg";
QString secondImagePath = "/home/nvidia/Desktop/TestFolder/secondImage.jpg";
QImage firstImage(firstImagePath);
QImage secondImage(secondImagePath);
QImage combinedImage;
QPainter paint(this);
paint.begin(&combinedImage);
paint.drawImage(0, 0, firstImage);
paint.drawImage(0, firstImage.height()*1.1, secondImage);
paint.end();
bool i = firstImage.save("/home/nvidia/Desktop/TestFolder/firstImage-Copy.bmp");
Q_ASSERT(i);
bool j = combinedImage.save("/home/nvidia/Desktop/TestFolder/combinedImage-Copy.bmp");
Q_ASSERT(j);
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void paintEvent(QPaintEvent *);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Here is the Qt output:
Starting /home/nvidia/qtprojects/build-CombinedSave-Jetson_TX2-Debug/CombinedSave...
QPainter::begin: Painter already active
ASSERT: "j" in file ../CombinedSave/mainwindow.cpp, line 39
The program has unexpectedly finished.
/home/nvidia/qtprojects/build-CombinedSave-Jetson_TX2-Debug/CombinedSave crashed
firstImage-Copy.bmp is stored in the TestFolder but combinedImage-Copy.bmp is not..
Some of my system and program properties:
ARMv8 Processor rev 3 (v8l) × 4 ARMv8 Processor rev 0 (v8l) × 2
Ubuntu 16.04 LTS
Qt Creator 3.5.1 Based on Qt 5.5.1 (GCC 5.2.1 20151129, 64 bit)
Consider the code...
QPainter paint(this);
paint.begin(&combinedImage);
The first line is essentially...
QPainter paint;
paint.begin(this);
and makes the QPainter
active on this
(your MainWindow
instance). But you then have...
paint.begin(&combinedImage);
which makes a second call to QPainter::begin
on the already active QPainter
. That's probably the source of the error message...
QPainter::begin: Painter already active
In addition, you also initialize combinedImage
using the default QImage
constructor...
QImage combinedImage;
At that point combinedImage
is a null image -- it has no size or format associated with it and cannot be used as-is.
To initialise and render combinedImage
try...
QImage combinedImage(std::max(firstImage.width(), secondImage.width()), /* Width */
firstImage.height() + secondImage.height(), /* Height */
QImage::Format_ARGB32_Premultiplied); /* Format */
{
QPainter paint(&combinedImage);
paint.drawImage(0, 0, firstImage);
paint.drawImage(0, firstImage.height(), secondImage);
}