Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Qt sending image between dialog

[SOLVED] Qt sending image between dialog

Scheduled Pinned Locked Moved General and Desktop
signal & slotqimageqdialog
9 Posts 2 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daguerreo
    wrote on 14 May 2015, 00:13 last edited by Daguerreo
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 May 2015, 20:52 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 16 May 2015, 22:46
      0
      • S SGaist
        15 May 2015, 20:52

        Hi and welcome to devnet,

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

        D Offline
        D Offline
        Daguerreo
        wrote on 16 May 2015, 22:46 last edited by
        #3

        @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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 May 2015, 22:50 last edited by
          #4

          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"

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 16 May 2015, 23:21
          0
          • S SGaist
            16 May 2015, 22:50

            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"

            D Offline
            D Offline
            Daguerreo
            wrote on 16 May 2015, 23:21 last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 17 May 2015, 21:11 last edited by
              #6

              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 ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply 18 May 2015, 13:32
              0
              • S SGaist
                17 May 2015, 21:11

                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 ?

                D Offline
                D Offline
                Daguerreo
                wrote on 18 May 2015, 13:32 last edited by
                #7

                @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 :\

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 18 May 2015, 20:37 last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  D 1 Reply Last reply 23 May 2015, 23:28
                  0
                  • S SGaist
                    18 May 2015, 20:37

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

                    D Offline
                    D Offline
                    Daguerreo
                    wrote on 23 May 2015, 23:28 last edited by
                    #9

                    @SGaist

                    Thank you, it has more sense now :)

                    1 Reply Last reply
                    0

                    1/9

                    14 May 2015, 00:13

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved