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:
Code:
...
topmostAction->setCheckable(true);
topmostAction->setChecked(false);
connect(topmostAction, SIGNAL(toggled(bool)),
this, SLOT(setTopmost(bool)));
addAction(topmostAction);
...
and later on the function that should be called:
Code:
void OvenTimerWidget::setTopmost(bool bTopmost)
{
Qt::WindowFlags flags;
flags = windowFlags();
if (bTopmost)
flags |= Qt::WindowStaysOnTopHint;
else
flags &= (!Qt::WindowStaysOnTopHint);
setWindowFlags(flags);
emit topmostChanged(bTopmost);
}
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!
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.
Re: toggled(bool) signal does not call my slot?
Also is flags &= (!Qt::WindowStaysOnTopHint); correct ?
shouldnt it be
flags &= (^Qt::WindowStaysOnTopHint); :rolleyes:
Re: toggled(bool) signal does not call my slot?
Quote:
Originally Posted by
aamer4yu
Also is flags &= (!Qt::WindowStaysOnTopHint); correct ?
shouldnt it be
flags &= (^Qt::WindowStaysOnTopHint); :rolleyes:
Ehm, no :D It should be
Code:
flags &= ~Qt::WindowStaysOnTopHint;
But better just toogle the hint then you don't need the if/else:
Code:
flags ^= Qt::WindowStaysOnTopHint;
Re: toggled(bool) signal does not call my slot?
Quote:
flags &= ~Qt::WindowStaysOnTopHint
Ohh yeah :crying:
I mean negate only.. guess my mind swapped symbols for negate and xor :D
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"
Code:
#ifndef TESTWINDOWTOPMOST_H
#define TESTWINDOWTOPMOST_H
#include <QWidget>
class TestWindowTopmost
: public QWidget{
Q_OBJECT
public:
TestWindowTopmost
(QWidget *parent
= 0);
public slots:
void setTopmost(bool);
};
#endif // TESTWINDOWTOPMOST_H
Source file "testtopmostwindow.cpp":
Code:
#include "testwindowtopmost.h"
#include <QAction>
TestWindowTopmost
::TestWindowTopmost(QWidget *parent
) : QWidget(parent
){
topmostAction->setCheckable(true);
topmostAction->setChecked(false);
connect(topmostAction, SIGNAL(toggled(bool)),
this, SLOT(setTopmost(bool)));
addAction(topmostAction);
setContextMenuPolicy(Qt::ActionsContextMenu);
setWindowTitle(tr("Test window topmost"));
}
void TestWindowTopmost::setTopmost(bool bTopmost)
{
// For now, just attempt to set a sensible base flag, like in the windowflags example
setWindowFlags(Qt::Dialog);
}
"main.cpp":
Code:
#include <QtGui/QApplication>
#include "testwindowtopmost.h"
int main(int argc, char *argv[])
{
TestWindowTopmost w;
w.resize(200,200);
w.show();
return a.exec();
}
Any ideas?
Re: toggled(bool) signal does not call my slot?
Found my mistake: Forgot the show() after setting the flags.