[SOLVED] Qt sending image between dialog



  • 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


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    One thing I see is that you never add your mPixmapItem to the scene



  • @SGaist
    Thank you for reply, but I don't understand what your point is. The visualization in both dialogs is correct, and I can send the image to the second one and process it


  • Lifetime Qt Champion

    The point was that you were setting the pixmap on an item that is not even associated to scene. On the other hand, you are adding a new pixmap each time the function is called.

    Then I might have misunderstood what you meant with "I try to send back the image"



  • @SGaist

    There is a communication between dialog A and B:
    A load an image
    On_click a button a signal is emitted, and the image is sent to B
    B manipulate the image and show it.

    Code work until here,

    now I try with another signal to send the manipulate image from dialog B to A, exactly like I did before, but something goes wrong.

    About the pixmap I don't understand well (because I'm a noob) where is the problem and what you're trying to focus on

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

    Maybe you could suggest the right way to do it?


  • Lifetime Qt Champion

    You have mPixmapItem that you seem to not use in your scene, but after that you keep adding new pixmaps on top of each others to the scene, is that really what you want ?



  • @SGaist

    Nope, indeed I don't know how it overlaps the images, and that mScene.clear() was to avoid that. I actually don't understand what is the right procedure :\


  • Lifetime Qt Champion

    Just add a QGraphicsPixmapItem to the scene and update the pixmap from that item



  • @SGaist

    Thank you, it has more sense now :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.