ich möchte ein kleines App programmieren, welches sich beim schliessen nicht beendet, sondern zu einem Trayicon minimiert. Soweit so gut, es scheint auch halbwegs zu funktionieren, aber jedesmal wenn ich die hide() methode (z.b. in closeEvent) )zum verstecken des Fensters aufrufen will, schliesst sich das komplette Programm. Ich weiss nicht warum, ich sehs einfach nicht. Vielleicht hatte hier jemand schon dieses Problem und kann mir da weiterhelfen. Ich habe mal den relevanten Source angehängt.
Code: Alles auswählen
#include <QApplication>
#include <QtGui>
#include "tabdialog.h"
TabDialog *tabdialog;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QCoreApplication::addLibraryPath ("./imageformats/");
// Q_INIT_RESOURCE(tabdialog);
// app.setQuitOnLastWindowClosed(false);
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
QMessageBox::critical(0, QObject::tr("Systray"), QObject::tr("I couldn't detect any system tray on this system."));
return 1;
}
tabdialog = new TabDialog();
tabdialog->setWindowTitle("Blah");
tabdialog->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
return tabdialog->exec();
}
Code: Alles auswählen
#ifndef TABDIALOG_H
#define TABDIALOG_H
#include <QDialog>
#include <QSystemTrayIcon>
#include <QTreeWidget>
#include <QTextEdit>
class QDialogButtonBox;
class QTabWidget;
class QAction;
class QMenu;
class ConfigTab : public QTreeWidget
{
Q_OBJECT
public:
ConfigTab(QWidget *parent = 0);
private:
};
class LogTab : public QWidget
{
Q_OBJECT
public:
LogTab(QWidget *parent = 0);
QTextEdit *textEdit;
private:
};
class TabDialog : public QDialog
{
Q_OBJECT
public:
TabDialog(QWidget *parent = 0);
QTabWidget *tabWidget;
LogTab *logtab;
protected:
void closeEvent(QCloseEvent *e);
void hideEvent(QHideEvent *h);
private slots:
void slotToggleVisibility();
void slotTrayActivated(QSystemTrayIcon::ActivationReason reason);
private:
QDialogButtonBox *buttonBox;
void createActions();
void createTrayIcon();
QAction *minimizeAction;
QAction *maximizeAction;
QAction *restoreAction;
QAction *quitAction;
QSystemTrayIcon *trayIcon;
QMenu *trayIconMenu;
};
#endif
Code: Alles auswählen
#include <QtGui>
#include "tabdialog.h"
TabDialog::TabDialog(QWidget *parent)
: QDialog(parent)
{
createActions();
createTrayIcon();
tabWidget = new QTabWidget;
tabWidget->addTab(new ConfigTab(), tr("Config"));
logtab = new LogTab();
tabWidget->addTab(logtab, tr("Log"));
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(tabWidget);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
}
ConfigTab::ConfigTab(QWidget *parent)
: QTreeWidget(parent)
{
QStringList labels;
labels << tr("Setting") << tr("Type") << tr("Value");
setHeaderLabels(labels);
header()->setResizeMode(0, QHeaderView::Stretch);
header()->setResizeMode(2, QHeaderView::Stretch);
}
LogTab::LogTab(QWidget *parent)
: QWidget(parent)
{
textEdit = new QTextEdit;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit);
setLayout(layout);
}
void TabDialog::createActions()
{
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(showMinimized()));
maximizeAction = new QAction(tr("Ma&ximize"), this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void TabDialog::hideEvent(QHideEvent *h)
{
h->accept();
trayIcon->show();
}
void TabDialog::closeEvent(QCloseEvent *e)
{
// scheint zu funktionieren, nur hide nicht, komisch...
// showMinimized();
hide();
e->ignore();
}
void TabDialog::slotToggleVisibility()
{
if(isHidden())
{
show();
if(isMinimized())
{
if(isMaximized())
showMaximized();
else
showNormal();
}
raise();
activateWindow();
}
else
{
hide();
}
}
void TabDialog::slotTrayActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
slotToggleVisibility();
break;
case QSystemTrayIcon::MiddleClick:
default:
;
}
}
void TabDialog::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon("red_up.png"));
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotTrayActivated(QSystemTrayIcon::ActivationReason)));
}