PDA

View Full Version : QMessageBox kill program if parent QWidget is hidden??



John Smith
24th January 2010, 09:57
QMessagebox kill program if parent QWidget is hidden. (Qt 4.6)

main.cpp

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

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
//w.hide(); // #1
return a.exec();
}

widget.cpp

#include "widget.h"
#include <QApplication>
#include <QAction>
#include <QSystemTrayIcon>
#include <QMenu>

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QAction *aboutQtAction = new QAction(tr("&About Qt"), this);
QObject::connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
QMenu *trayMenu = new QMenu(this);
trayMenu->addAction(aboutQtAction);
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayMenu);
trayIcon->show();

//setWindowFlags(Qt::Desktop); // #2
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = 0);
~Widget();
};

#endif // WIDGET_H

If line #1 or #2 uncommented, when I close QMessagebox (aboutQt in this example), my program crashes.

Why my program crashes and how can I fix it?

wysota
24th January 2010, 13:46
Why my program crashes and how can I fix it?
Don't set your widget type to Qt::Desktop. You should never set this flag yourself.

wysota
24th January 2010, 13:46
Why my program crashes and how can I fix it?
Don't set your widget type to Qt::Desktop. You should never set this flag yourself.