Seite 1 von 1

[gelöst] QSystemTrayIcon - hide() funktioniert nicht

Verfasst: 13. April 2007 18:10
von buggybear
Hallo,

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)));
}


Verfasst: 13. April 2007 18:25
von Christian81
Und warum ist "app.setQuitOnLastWindowClosed(false); " auskommentiert?

Verfasst: 13. April 2007 18:27
von buggybear
Christian81 hat geschrieben:Und warum ist "app.setQuitOnLastWindowClosed(false); " auskommentiert?
Weil es auch unkommentiert nicht das Problem löst. :)

Verfasst: 13. April 2007 19:01
von Christian81
buggybear hat geschrieben: Weil es auch unkommentiert nicht das Problem löst. :)
Ok, das ist doch schonmal ein Anfang. Auch wenn es irgendwie komisch ist.
Überschreib in deinem MainWindow QWidget::closeEvent() und schau ob es dann funktioniert.

Verfasst: 13. April 2007 19:20
von buggybear
Christian81 hat geschrieben: Ok, das ist doch schonmal ein Anfang. Auch wenn es irgendwie komisch ist.
Überschreib in deinem MainWindow QWidget::closeEvent() und schau ob es dann funktioniert.
Hmmm habe ich doch im Dialog schon überschrieben?

Code: Alles auswählen


void TabDialog::closeEvent(QCloseEvent *e)
{

   // scheint zu funktionieren, nur hide nicht, komisch...
//   showMinimized();
   hide();
   e->ignore();

} 

Oder meinst Du in der main.cc?

Das hier funktioniert z.b. auch nicht. Sobald hide() aufgerufen wird, beendet sich das Programm, ich kann machen was ich will...

Code: Alles auswählen

#include <QApplication>
#include <QtGui>
#include "tabdialog.h"

TabDialog *tabdialog;

void closeEvent(QCloseEvent *e)
{

	tabdialog->hide();
	e->ignore();

}

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();
}
Ich sitz da jetzt schon Stunden dran und weiss einfach nicht woran das liegt bzw. kanns mir nicht erklären.

Verfasst: 14. April 2007 08:39
von Volker
Hast du schon mal versucht statt tabDialog->exec nur tabDialog->show und danach app.exec() aufzurufen?
Denke nämlich, dass das app.setQuitOnLastWindowClosed(false); nur funktioniert, wenn du auch app.exec() aufrufst.

Verfasst: 14. April 2007 11:53
von buggybear
Volker hat geschrieben:Hast du schon mal versucht statt tabDialog->exec nur tabDialog->show und danach app.exec() aufzurufen?
Denke nämlich, dass das app.setQuitOnLastWindowClosed(false); nur funktioniert, wenn du auch app.exec() aufrufst.
Genau das war der Fehler! :) Es liegt an dem tabdialog->show(); app.exec; danach funktionierts.
Das app.setQuitOnLastWindowClosed(false); benötigt man auch nicht, d.h. es funktioniert auch ohne.

Vielen Dank für die Hilfe! :)