PDA

View Full Version : toggled(bool) signal does not call my slot?



Asperamanca
21st July 2010, 17:34
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:


...
QAction *topmostAction = new QAction("Stay on &Top", this);
topmostAction->setShortcut(QKeySequence("Ctrl+T"));
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:


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!

Lykurg
21st July 2010, 17:41
is setTopmost() defined as a slot and did you use the Q_OBJECT macro? Please show us your header file.

aamer4yu
22nd July 2010, 05:04
Also is flags &= (!Qt::WindowStaysOnTopHint); correct ?
shouldnt it be
flags &= (^Qt::WindowStaysOnTopHint); :rolleyes:

Lykurg
22nd July 2010, 06:06
Also is flags &= (!Qt::WindowStaysOnTopHint); correct ?
shouldnt it be
flags &= (^Qt::WindowStaysOnTopHint); :rolleyes:

Ehm, no :D It should be
flags &= ~Qt::WindowStaysOnTopHint; But better just toogle the hint then you don't need the if/else:
flags ^= Qt::WindowStaysOnTopHint;

aamer4yu
22nd July 2010, 06:38
flags &= ~Qt::WindowStaysOnTopHint
Ohh yeah :crying:

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

Asperamanca
22nd July 2010, 09:46
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"

#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":

#include "testwindowtopmost.h"
#include <QAction>

TestWindowTopmost::TestWindowTopmost(QWidget *parent) : QWidget(parent)
{
QAction *topmostAction = new QAction("Stay on &Top", this);
topmostAction->setShortcut(QKeySequence("Ctrl+T"));
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":

#include <QtGui/QApplication>
#include "testwindowtopmost.h"


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestWindowTopmost w;

w.resize(200,200);
w.show();
return a.exec();
}


Any ideas?

Asperamanca
22nd July 2010, 09:54
Found my mistake: Forgot the show() after setting the flags.