Results 1 to 2 of 2

Thread: Destruction of a slot's local variables in an event loop

  1. #1
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Destruction of a slot's local variables in an event loop

    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.

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent)
    2. {
    3. QPushButton *pb_browse = new QPushButton("...", this);
    4. QLineEdit *le_path = new QLineEdit(this);
    5.  
    6. // ...
    7.  
    8. connect(pb_browse, SIGNAL(clicked()),
    9. this, SLOT(selectFile()));
    10. connect(this, SIGNAL(selected(const QString &)),
    11. le_path, SLOT(setText(const QString &)));
    12. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // A slot
    2. void MyWidget::selectFile()
    3. {
    4. QString path = QFileDialog::getOpenFileName(this, "Select a file");
    5.  
    6. emit(selected(path));
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Destruction of a slot's local variables in an event loop

    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.

  3. The following user says thank you to Lesiok for this useful post:

    hackerNovitiate (21st June 2010)

Similar Threads

  1. Replies: 10
    Last Post: 15th January 2010, 15:35
  2. Event Loop started in slot function
    By elizabeth.h1 in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 13:56
  3. Replies: 0
    Last Post: 23rd October 2008, 13:43
  4. signal and slot across threads having event loop
    By travis in forum Qt Programming
    Replies: 6
    Last Post: 6th November 2007, 00:56
  5. Replies: 4
    Last Post: 23rd January 2006, 17:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.