0

I'm writing a simple application with 2 dialogs, the first for one original image, which is sent to another dialog and processed. Then the image processed is sent back to the first dialog:

ImageDialog::ImageDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ImageDialog)
{
    ui->setupUi(this);
    ui->graphicsView->setScene(&mScene);
    mPixmapItem = new QGraphicsPixmapItem();

    connect( this,      SIGNAL( sigCommitImage(QImage) ),
             parent,    SLOT( updateImage(QImage) ) );
}

ImageDialog::~ImageDialog()
{
    delete ui;
    delete mPixmapItem;
}

void ImageDialog::processImage(const QImage &image )
{
    cv::Mat tmp(image.height(),image.width(),CV_8UC4,(uchar*)image.bits(),image.bytesPerLine());
    cv::cvtColor(tmp, tmp, CV_RGBA2GRAY);

    mImage = QImage( (uchar*)tmp.data, tmp.cols, tmp.rows, tmp.step, QImage::Format_Indexed8 );

    mPixmapItem->setPixmap(QPixmap::fromImage(mImage));
    mScene.clear();
    mScene.addPixmap(QPixmap::fromImage(mImage));
    mScene.setSceneRect(0, 0, mImage.width(), mImage.height());
    ui->graphicsView->fitInView(mPixmapItem, Qt::KeepAspectRatio);
}

void ImageDialog::on_buttonBox_accepted()
{
    emit( sigCommitImage( mImage ) );
}

and there's a snippet for the main window

mPixelInfoDialog = new PixelInfoDialog( this );
connect( this,          SIGNAL( sigPixelInfoDialog(QPixmap, QPointF) ),
         mPixelInfoDialog, SLOT( updateClip(QPixmap, QPointF) ) );

mImageDialog = new ImageDialog( this );
connect( this,          SIGNAL( sigImageDialog(QImage) ),
     mImageDialog,  SLOT( processImage(QImage) ) );

void MainWindow::updateImage(const QImage &image)
{
    showImage( image );
}

void MainWindow::showImage(const QImage &image)
{
    mImage = image;
    mPixmapItem->setPixmap(QPixmap::fromImage(image));
    mScene.clear();
    mScene.addPixmap(QPixmap::fromImage(image));
    mScene.setSceneRect(0, 0, image.width(), image.height());
    ui->graphicsView->fitInView(mPixmapItem, Qt::KeepAspectRatio);
}

    void MainWindow::on_action_Open_triggered()
    {
        QString path = QFileDialog::getOpenFileName();

        if( path.isEmpty() )
        {
            return;
        }

        QImage image(path);
        if( image.isNull() )
            return;

        showImage( image );

        /*
         * Update other open dialogs
         */

        if( mImageDialog->isEnabled() )
        {
            emit( sigImageDialog( mImage ) );
        }

    }

The image is properly loaded, sent to ImageDialog and processed, but when I try to send back the image, the result is a bunch of random pixel or the application crash in

mPixmapItem->setPixmap(QPixmap::fromImage(image));

I can't figure what's happening because I'm doing the same operations. Any advice? Thanks in advance

  • Try to save image to file on every step and find out, when it becomes corrupted. For example, when you call `emit( sigCommitImage( mImage ) );`, is your `mImage` valid, or is it already bad? Also, try to rebuild project (sometimes helps, when working in QtCreator). – Amartel May 14 '15 at 07:51
  • The image is not corrupted because I show it in the second dialog, and it's displayed correctly – user3489057 May 15 '15 at 09:40

0 Answers0