Results 1 to 7 of 7

Thread: toggled(bool) signal does not call my slot?

  1. #1
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question toggled(bool) signal does not call my slot?

    Hi,

    I have a toggle action that should call a function within my class when toggled.
    However, it does not, and I have no idea why not.

    Could someone please take a look?

    In my widget constructor:
    Qt Code:
    1. ...
    2. QAction *topmostAction = new QAction("Stay on &Top", this);
    3. topmostAction->setShortcut(QKeySequence("Ctrl+T"));
    4. topmostAction->setCheckable(true);
    5. topmostAction->setChecked(false);
    6. connect(topmostAction, SIGNAL(toggled(bool)),
    7. this, SLOT(setTopmost(bool)));
    8.  
    9. addAction(topmostAction);
    10. ...
    To copy to clipboard, switch view to plain text mode 

    and later on the function that should be called:

    Qt Code:
    1. void OvenTimerWidget::setTopmost(bool bTopmost)
    2. {
    3. Qt::WindowFlags flags;
    4.  
    5. flags = windowFlags();
    6.  
    7. if (bTopmost)
    8. flags |= Qt::WindowStaysOnTopHint;
    9. else
    10. flags &= (!Qt::WindowStaysOnTopHint);
    11.  
    12. setWindowFlags(flags);
    13.  
    14. emit topmostChanged(bTopmost);
    15. }
    To copy to clipboard, switch view to plain text mode 

    I have my breakpoint in there, click on the (context) menu entry, it gets checked, but I never reach the breakpoint. What could be wrong here?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: toggled(bool) signal does not call my slot?

    is setTopmost() defined as a slot and did you use the Q_OBJECT macro? Please show us your header file.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toggled(bool) signal does not call my slot?

    Also is flags &= (!Qt::WindowStaysOnTopHint); correct ?
    shouldnt it be
    flags &= (^Qt::WindowStaysOnTopHint);

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: toggled(bool) signal does not call my slot?

    Quote Originally Posted by aamer4yu View Post
    Also is flags &= (!Qt::WindowStaysOnTopHint); correct ?
    shouldnt it be
    flags &= (^Qt::WindowStaysOnTopHint);
    Ehm, no It should be
    Qt Code:
    1. flags &= ~Qt::WindowStaysOnTopHint;
    To copy to clipboard, switch view to plain text mode 
    But better just toogle the hint then you don't need the if/else:
    Qt Code:
    1. flags ^= Qt::WindowStaysOnTopHint;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toggled(bool) signal does not call my slot?

    flags &= ~Qt::WindowStaysOnTopHint
    Ohh yeah

    I mean negate only.. guess my mind swapped symbols for negate and xor

  6. #6
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: toggled(bool) signal does not call my slot?

    Sure enough, I forgot to declare that function a slot. Thanks!

    Now I just have to find out why actually calling that slot kills my application. Can something like setting window flags not be done from within the context of a context menu of that window?

    I have boiled it down to a very simple sample program. When you run it, open the context menu and select "Stay on Top", the window disappears (even from the taskbar), and I have to kill the application using the task manager or debugger.

    Why?

    Header file "testtopmostwindow.h"
    Qt Code:
    1. #ifndef TESTWINDOWTOPMOST_H
    2. #define TESTWINDOWTOPMOST_H
    3.  
    4. #include <QWidget>
    5.  
    6.  
    7. class TestWindowTopmost : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. TestWindowTopmost(QWidget *parent = 0);
    13.  
    14. public slots:
    15. void setTopmost(bool);
    16. };
    17.  
    18. #endif // TESTWINDOWTOPMOST_H
    To copy to clipboard, switch view to plain text mode 

    Source file "testtopmostwindow.cpp":
    Qt Code:
    1. #include "testwindowtopmost.h"
    2. #include <QAction>
    3.  
    4. TestWindowTopmost::TestWindowTopmost(QWidget *parent) : QWidget(parent)
    5. {
    6. QAction *topmostAction = new QAction("Stay on &Top", this);
    7. topmostAction->setShortcut(QKeySequence("Ctrl+T"));
    8. topmostAction->setCheckable(true);
    9. topmostAction->setChecked(false);
    10. connect(topmostAction, SIGNAL(toggled(bool)),
    11. this, SLOT(setTopmost(bool)));
    12. addAction(topmostAction);
    13.  
    14. setContextMenuPolicy(Qt::ActionsContextMenu);
    15. setWindowTitle(tr("Test window topmost"));
    16. }
    17.  
    18. void TestWindowTopmost::setTopmost(bool bTopmost)
    19. {
    20. // For now, just attempt to set a sensible base flag, like in the windowflags example
    21. setWindowFlags(Qt::Dialog);
    22. }
    To copy to clipboard, switch view to plain text mode 

    "main.cpp":
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "testwindowtopmost.h"
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. TestWindowTopmost w;
    9.  
    10. w.resize(200,200);
    11. w.show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas?

  7. #7
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: toggled(bool) signal does not call my slot?

    Found my mistake: Forgot the show() after setting the flags.

Similar Threads

  1. Signal/slot or direct method call within a class
    By mike_the_tv in forum Newbie
    Replies: 6
    Last Post: 11th March 2010, 18:49
  2. Doxgen call graphs also for signal-slot calls?
    By zavulon in forum Qt Programming
    Replies: 0
    Last Post: 25th September 2008, 15:29
  3. QGroupBox and signal clicked(bool)
    By codebehind in forum Newbie
    Replies: 9
    Last Post: 30th August 2008, 21:09
  4. Replies: 0
    Last Post: 23rd September 2007, 11:54
  5. No such signal QRadioButton::toggled()
    By bpetty in forum Newbie
    Replies: 3
    Last Post: 15th August 2006, 13:22

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.