jwm
jwm

Reputation: 5030

how to access Qt MainWindow members

In my project, I want to call a variable QImage image generated in the mainwindow.h, in other class files, e.g., called SegSetupDialog.h. Here the image is loaded by clicking a pushbutton in mainwindow.ui, and the SegSetupDialog is a QDialog, which pops up by clicking one pushbutton in mainwindow.ui.

I tried to use a signal-slot connection to send qimage of mainwindow to SegSetupDialog as follows.

For Class MainWindow:

SegSetupDialog *segsetup;

if(image.isNull()==false)
  {
        emit sendImgData(image);
        qDebug()<<"sendImgData emitted!";

        if(segsetup==NULL) segsetup = new SegSetupDialog();
        connect(this, SIGNAL(sendImgData(QImage)),segsetup,SLOT(getImgData(QImage)),Qt::QueuedConnection);
   }

In SegSetupDialog::getImgData

void SegSetupDialog::getImgData(QImage qimage)
{
    qImg = qimage;
    qDebug()<<"qimage received!";
}

The above connection seems not to work since the qDebug message in getImgData is not printed out. Anyone can help check something wrong with codes, or suggest other methods for accessing image of mainwindow? Thanks!!

Upvotes: 0

Views: 77

Answers (1)

Nikos C.
Nikos C.

Reputation: 51832

You need to do the signal/slot connection before you emit the signal. Connections are done once, usually in the constructor.

However, you should probably do the connect() in the constructor of SegSetupDialog instead of the MainWindow one. It's SegSetupDialog that wants to be notified about image data updates, so you should establish the connection there.

Also, to make sure the signals and slots are specified correctly, don't use Qt 4 connect() calls. Use Qt 5 ones that are checked at compile-time:

connect(this, &MainWindow::sendImgData,
        segsetup, &SegSetupDialog::getImgData, Qt::QueuedConnection);

(Of course change it appropriately if you move it to the SegSetupDialog constructor.)

Upvotes: 1

Related Questions