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