PDA

View Full Version : Signal and Slot Problem



waynew
5th June 2010, 01:07
Just when I thought I had signals and slots figured out ... not so.
I have one class (editlog) that sends a signal to a slot in the mainwindow, and it works fine.
So I set up another one from a different class (openlog), and ... nothing, no response.

Here is the code that works ok:



// part of editlog.h
signals:
void addLookup(int);

// part of editlog.cpp
void EditLog::on_pushButton_clicked()
{
emit addLookup(::logID);
}

// part of mainwindow.h
public:
EditLog *el;

public slots:
void updateLog(int);

// part of mainwindow.cpp

el = new EditLog;
// Signal connection from EditLog class
connect(el, SIGNAL(addLookup(int)), this, SLOT(updateLog(int)));

void MainWindow::updateLog(int newID)
{
// code updates log ok
}


Here is the code that seems to do nothing:


// part of openlog.h // this is a dialog

signals:
void callOpenLog(QString);

// part of openlog.cpp

void OpenLog::on_pbOpen_clicked()
{
QString openLog = ui->cbLogName->currentText();
emit callOpenLog(openLog);
close(); // the dialog window
}

// part of mainwindow.h
public:
OpenLog *ol;

// part of mainwindow.cpp
ol = new OpenLog;
connect(ol, SIGNAL(callOpenLog(QString)), this, SLOT(openLog(QString)));

void MainWindow::openLog(QString logName) // never called
{
// code would open the log if the signal was received
}


Could this have something to do with the fact that the signal from the class that works comes from a dialog window that does not get closed after the signal is sent and the one that does not work comes from a dialog class where the window gets closed right after the signal is sent. If so, how to make it work? Or what else is wrong?

Zlatomir
5th June 2010, 04:22
Well, just try without close(); and see what happens.

I'm not sure about this, but your widget might be deleted on close(), if it works, you can use hide(); and the widget will be deleted by the parent.

ChrisW67
5th June 2010, 07:35
Dumb mistakes I typically make... Has MainWindow::openLog() been declared as a slot? Are any error messages regarding the connect() call output at run time?

waynew
5th June 2010, 10:49
Well, I tried the hide() on the dialog instead of close(). Same result, no action.
Sorry Chris, I was afraid I would forget to post some part of the code. Yes, openLog(QString) is declared as a public slot in mainwindow.h, however I have forgotten that in the past too.

What really puzzles me is that the signal from editlog works perfectly. But the structure between the techniques used in editlog and openlog seem to be identical. Got to be some simple mistake.

UPDATE: problem solved. Yep, it was a simple mistake.
the openlog dialog was created on the stack. when I switched it to the heap and moved the connect statement to the code where it is created, all works fine.
Thanks for your ideas.