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
#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
To copy to clipboard, switch view to plain text mode
Source file "testtopmostwindow.cpp":
#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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
"main.cpp":
#include <QtGui/QApplication>
#include "testwindowtopmost.h"
int main(int argc, char *argv[])
{
TestWindowTopmost w;
w.resize(200,200);
w.show();
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Any ideas?
Bookmarks