PDA

View Full Version : Destruction of a slot's local variables in an event loop



hackerNovitiate
21st June 2010, 07:02
Hi all,

Is it safe to assume that a slot's local variables will be valid until the end of the event loop?

For example, is it safe to emit a reference to a local variable (see below)? All of Qt's official examples (that I've seen) for signals/slots that involve QString seem to pass them by reference. My short tests with this code also seemed to work fine in Windows 7 and Linux, but I can't help wondering if there's a risk that selectFile() will return (and "path" gets destroyed) before the string can be processed.


MyWidget::MyWidget(QWidget *parent)
{
QPushButton *pb_browse = new QPushButton("...", this);
QLineEdit *le_path = new QLineEdit(this);

// ...

connect(pb_browse, SIGNAL(clicked()),
this, SLOT(selectFile()));
connect(this, SIGNAL(selected(const QString &)),
le_path, SLOT(setText(const QString &)));
}
// A slot
void MyWidget::selectFile()
{
QString path = QFileDialog::getOpenFileName(this, "Select a file");

emit(selected(path));
}

Lesiok
21st June 2010, 08:26
When connection signal/slot is created as not Qt::QueuedConnection then emit signal is equivalent of calling method (slot). When connection is creted as Qt::QueuedConnection then signal engine creates copy of all parameters. Therefore in Qt::QueuedConnection signals You can use only registered meta types.