Qt在对话框之间发送图像

时间:2015-05-14 00:08:15

标签: c++ qt qimage qt-signals

我正在编写一个带有2个对话框的简单应用程序,第一个用于原始图像,然后发送到另一个对话框并进行处理。然后处理的图像被发送回第一个对话框:

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

并且主窗口有一个片段

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

    }

图像已正确加载,发送到ImageDialog并进行处理,但当我尝试发回图像时,结果是一堆随机像素或应用程序崩溃

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

我无法弄清楚发生了什么,因为我正在做同样的操作。 有什么建议? 提前致谢

0 个答案:

没有答案