简体   繁体   English

无法存储(保存)QPainter图像

[英]Can't Store(Save) the QPainter Image

I'm combining 2 QImages ("firstImage" and "secondImage") vertically with the help of QPainter. 我正在QPainter的帮助下垂直组合2个QImages(“ firstImage”和“ secondImage”)。 Combined image displays properly, with no error, as seen from the screen shot: 从屏幕截图中可以看出,组合图像正确显示,没有错误:

Combined Image Displays Properly 正确组合图像显示

First Image 第一张图片

Second Image 第二张图片

But if I want to store the "combinedImage" by using .save, program halts and I get assert error. 但是,如果我想使用.save存储“ combinedImage”,程序会暂停,并且会出现断言错误。

How can I store the combined image into the harddisk? 如何将合并的图像存储到硬盘中?

mainwindow.cpp: 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: 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: 这是Qt输出:

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.. firstImage-Copy.bmp存储在TestFolder中,但combinedImage-Copy.bmp不存储。

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). 并使QPainter this (您的MainWindow实例)处于活动状态。 But you then have... 但是你有...

paint.begin(&combinedImage);

which makes a second call to QPainter::begin on the already active QPainter . 在已经激活的QPainter二次调用QPainter::begin That's probably the source of the error message... 这可能是错误消息的来源...

QPainter::begin: Painter already active QPainter :: begin:画家已经激活

In addition, you also initialize combinedImage using the default QImage constructor... 此外,您还可以使用默认的QImage构造函数初始化combinedImage

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. 此时, combinedImage是空图像-它没有大小或格式与之关联,因此不能原样使用。

To initialise and render combinedImage try... 要初始化和渲染combinedImage尝试...

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM