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
){
// ...
connect(pb_browse, SIGNAL(clicked()),
this, SLOT(selectFile()));
connect(this,
SIGNAL(selected
(const QString &)),
le_path,
SLOT(setText
(const QString &)));
}
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 &)));
}
To copy to clipboard, switch view to plain text mode
// A slot
void MyWidget::selectFile()
{
emit(selected(path));
}
// A slot
void MyWidget::selectFile()
{
QString path = QFileDialog::getOpenFileName(this, "Select a file");
emit(selected(path));
}
To copy to clipboard, switch view to plain text mode
Bookmarks