【问题标题】:Send image path to another function将图像路径发送到另一个函数
【发布时间】:2013-09-05 06:59:01
【问题描述】:

我在 Qt 中有一个 mainWindow 和一个 Dialog 。我在 MainWindow 打开两个图像。在我对 MainWindow 上的图像(裁剪、调整大小、旋转)进行操作之后。我想将图像发送到另一个 window (QDialog) 。我如何将其作为parameter 发送?我的部分代码如下;

MainWindow::MainWindow()
{
   openButton_1 = new QPushButton(tr("Open"));
   cropButton_1 = new QPushButton(tr("Crop"));
   rotateButton_1 = new QPushButton(tr("Rotate"));
   resizeButton_1 = new QPushButton(tr("Resize"));
   doneButton = new QPushButton(tr("Done"));

   ....
   ....
   ....
   ....
   ....


   connect(openButton_1, SIGNAL(clicked()), this, SLOT(open1()));
   connect(openButton_2, SIGNAL(clicked()), this, SLOT(open2()));

   connect(doneButton, SIGNAL(clicked()), this, SLOT(done()));

//打开新窗口的done()函数

void MainWindow::done()
{
    CompareImage dialog(this);
    dialog.exec();

}

//新的对话窗口

CompareImage::CompareImage( QWidget *parent ) : QDialog( parent )
{
   pushButton = new QPushButton(tr("TesT"));

   graphicsScene = new QGraphicsScene;
   graphicsView = new QGraphicsView(graphicsScene);



   QVBoxLayout *mainLayout = new QVBoxLayout;
   mainLayout->addWidget( pushButton );
   mainLayout->addWidget( graphicsView );
   setLayout( mainLayout );
}

// 这里还有我的 open() 函数

void MainWindow::open( int signal )
 {
     QString fileName = QFileDialog::getOpenFileName(this,
                                     tr("Open File"), QDir::currentPath());
     if (!fileName.isEmpty()) {
         QImage image(fileName);
         if (image.isNull()) {
             QMessageBox::information(this, tr("Image Viewer"),
                                      tr("Cannot load %1.").arg(fileName));
             return;
         }
         QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));

         if( signal == 1 )
         {
             graphicsScene_1->addItem(item);
             graphicsView_1->show();
         }
         else if(signal == 2)
         {
             graphicsScene_2->addItem(item);
             graphicsView_2->show();
         }

     }
 }

使用QGraphicsPixmapItem* item 看起来不错,但我做不到.. 你能帮帮我吗?谢谢你的想法..

> 编辑:这里也是我的 open1 和 open2 函数,可以清楚地了解情况..

void MainWindow::open1()
{
    open( 1 );
}

void MainWindow::open2()
{
    open( 2 );
}

【问题讨论】:

  • 我不知道你是如何发送信号来打开函数的?调用 open 时信号的值是多少?
  • 我正在使用两个公共插槽:open1() 和 open2() 因此我可以发送信号.. 我不使用按钮调用 Open 函数。我在另一个函数中调用 open() 函数,其值如 open(1) 或 open(2)
  • It looks good idea to use QGraphicsPixmapItem* item but i couldnt make it 你做不到是什么意思?
  • @thuga 我尝试使用项目对象作为 QDialog 窗口的参数,但我无法做到。
  • @goGud 为什么?发生了什么?你到底尝试了什么?

标签: c++ image qt qmainwindow qdialog


【解决方案1】:

最好的方法是使用信号/槽
1.在主窗口声明中添加类似:

signals:
    void ImageProcessingDone(QImage& image);

2。在您的对话框中声明插槽

public slosts:
     void RecevedProcessedImage(QImage& image);

3。用于处理图像的实现槽。
4. 在主窗口的构造中连接信号和槽。
因此,当您的图像处理完成时,只需在 MainWindow 中编写 emit ImageProcessingDone(imageInstance) ,它将被传输到您的对话框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多