Results 1 to 7 of 7

Thread: unexpexted SIGNAL/SLOT behavior

  1. #1
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default unexpexted SIGNAL/SLOT behavior

    I have a situation where a signal needs to be emitted twice before it triggers slot action. Does anyone know why? This is what I have:

    *.h file
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. .............
    5. public slots:
    6. void reSize();
    7. signals:
    8. void getSize();
    9. ...........
    10. }
    To copy to clipboard, switch view to plain text mode 

    constructor in main.cpp
    Qt Code:
    1. ................
    2. connect(this, SIGNAL(getSize()), this, SLOT(reSize()));
    3. ...............
    To copy to clipboard, switch view to plain text mode 

    functions.cpp
    Qt Code:
    1. ...........
    2. void MyWidget::function1()
    3. {
    4. emit getSize();
    5. emit getSize();//both emit's are needed!!
    6. }
    7.  
    8. void MyWidget::reSize()
    9. {
    10. resize(x,y);//x and y are global variables
    11. }
    12. ...........
    To copy to clipboard, switch view to plain text mode 

    The amazing thing is that I need to emit the signal twice. If it is emitted once, the resize function (in function2()) is not executed. ??
    Last edited by eric; 21st January 2008 at 22:38.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unexpexted SIGNAL/SLOT behavior

    What does function2() have to do with this signal-slot connection? Are you also mixing QWidget::resize() and MyWidget::reSize()?
    J-P Nurmi

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

    eric (21st January 2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unexpexted SIGNAL/SLOT behavior

    Hi JPN,

    Very sorry, my typo! I corrected it in the first post now. There is no function2(). Sorry again.

    Are you also mixing QWidget::resize() and MyWidget::reSize()?
    No, I don't mix resize() with reSize().

    The above code works fine but I am confused why I have to emit signal twice.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unexpexted SIGNAL/SLOT behavior

    Could you provide a minimal compilable example which reproduces the problem?
    J-P Nurmi

  6. #5
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unexpexted SIGNAL/SLOT behavior

    This is, indeed, the best way to try to solve the mystery. The problem is that my program is very many functions long, all interacting with one another. It's very difficult to make a minimal program out of it. Besides, the minimal signal/slot without all the extra load always works fine.
    My suspicion is that the signal needs to be emitted twice because the program is busy with something else and doesn't "notice" the signal. I have tried emitting the signal from various locations and also combining it with "QApplication::processEvents();" but no effect.
    Is there a better way to increase priority of something in another way than doing: "QApplication::processEvents();"
    Last edited by jpn; 21st January 2008 at 23:19. Reason: disabled smilies

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: unexpexted SIGNAL/SLOT behavior

    I suggest taking a debugger and stepping through the code. I suspect you'll be able to find your problem easily then.

  8. #7
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unexpexted SIGNAL/SLOT behavior

    if this is really in your constructor:
    Qt Code:
    1. connect(this, SIGNAL(getSize()), this, SLOT(reSize()));
    To copy to clipboard, switch view to plain text mode 
    Then the signal thats being emitted lives in the object thats also recieving it. So why don't you just call the slot directly?

    so... your functions.cpp file now has:
    Qt Code:
    1. void MyWidget::function1()
    2. {
    3. reSize();
    4. }
    5.  
    6. void MyWidget::reSize()
    7. {
    8. resize(x,y);//x and y are global variables
    9. }
    10. ...........
    To copy to clipboard, switch view to plain text mode 

    Paul

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.