Got the app hiding in the system tray, though icon isn't working.

When you hit the close button on the window, it hides in the system
tray, or when you hit File>Hide or Ctrl+H while the application has
focus.  I have some testing code in to see why the icon isn't being set,
but still can't figure it out.  The icon is definitely loading, it's
just when I set it to the icon it's no visible.  I'm even not getting
console output saying no icon is being set.
This commit is contained in:
Brian 2013-12-23 01:43:10 -07:00
parent 003c8a1157
commit c7914480e0
6 changed files with 124 additions and 36 deletions

0
code/bin/srv/pingme.txt Normal file
View File

View File

@ -18,38 +18,5 @@ HEADERS += ../src/mainwindow.h
FORMS += ../src/mainwindow.ui
OTHER_FILES += \
res/arrow_fat_down.gif \
res/arrow_fat_up.gif \
res/arrow_mini_down.gif \
res/arrow_mini_up.gif \
res/block.gif \
res/cross.gif \
res/drop_box.gif \
res/folder.gif \
res/plus.gif \
res/save.gif \
res/script.gif \
res/trash.gif \
res/warning.gif \
res/play.png \
res/settings.png \
res/stock_3d-texture-and-shading_16x16.png \
res/stock_3d-texture-and-shading_32x32.png \
res/stock_3d-texture-and-shading_48x48.png \
res/stock_3d-texture-and-shading_128x128.png \
res/stock_3d-texture-and-shading_256x256.png \
res/stock_3d-texture-spherical_16x16.png \
res/stock_3d-texture-spherical_32x32.png \
res/stock_3d-texture-spherical_48x48.png \
res/stock_3d-texture-spherical_128x128.png \
res/stock_3d-texture-spherical_256x256.png \
res/stop.png \
res/stock_3d-texture-and-shading_16x16.ico \
res/stock_3d-texture-and-shading_32x32.ico \
res/stock_3d-texture-and-shading_128x128.ico \
res/stock_3d-texture-and-shading_256x256.ico \
res/stock_3d-texture-spherical_16x16.ico \
res/stock_3d-texture-spherical_32x32.ico \
res/stock_3d-texture-spherical_128x128.ico \
res/stock_3d-texture-spherical_256x256.ico
RESOURCES += \
res.qrc

22
code/build/res.qrc Normal file
View File

@ -0,0 +1,22 @@
<RCC>
<qresource prefix="/images">
<file>res/arrow_fat_down.gif</file>
<file>res/arrow_fat_up.gif</file>
<file>res/arrow_mini_down.gif</file>
<file>res/arrow_mini_up.gif</file>
<file>res/block.gif</file>
<file>res/cross.gif</file>
<file>res/drop_box.gif</file>
<file>res/folder.gif</file>
<file>res/play.png</file>
<file>res/plus.gif</file>
<file>res/save.gif</file>
<file>res/script.gif</file>
<file>res/settings.png</file>
<file>res/stop.png</file>
<file>res/trash.gif</file>
<file>res/warning.gif</file>
<file>res/stock_3d-texture-and-shading_16x16.png</file>
<file>res/stock_3d-texture-spherical_16x16.png</file>
</qresource>
</RCC>

View File

@ -1,14 +1,91 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCloseEvent>
#include <QResource>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//QResource::registerResource("res.qrc");
ui->setupUi(this);
m_pActiveIcon = new QPixmap(":/images/res/stock_3d-texture-and-shading_16x16.png");
m_pInactiveIcon = new QPixmap(":/images/res/stock_3d-texture-spherical_16x16.png");
const bool activeNotNull = QIcon(":/images/res/stock_3d-texture-and-shading_16x16.png").isNull();
const bool inactiveNotNull = QIcon(":/images/res/stock_3d-texture-spherical_16x16.png").isNull();
m_pExitAction = new QAction(tr("&Exit"), this);
connect(m_pExitAction, SIGNAL(triggered()), this, SLOT(exit()));
m_pTrayIconMenu = new QMenu(this);
m_pTrayIconMenu->addAction(m_pExitAction);
m_pTrayIcon = new QSystemTrayIcon(this);
m_pTrayIcon->setContextMenu(m_pTrayIconMenu);
m_pTrayIcon->setIcon(QIcon(":/images/res/stock_3d-texture-and-shading_16x16.png"));
m_pTrayIcon->show();
connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
connect(ui->actionHide, SIGNAL(triggered()), this, SLOT(hide()));
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(exit()));
connect(
m_pTrayIcon,
SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
MainWindow::~MainWindow()
{
delete m_pInactiveIcon;
delete m_pActiveIcon;
delete m_pExitAction;
delete m_pTrayIcon;
delete m_pTrayIconMenu;
delete ui;
}
void MainWindow::save()
{
}
void MainWindow::exit()
{
qApp->exit();
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch(reason){
case QSystemTrayIcon::Trigger:
break;
case QSystemTrayIcon::DoubleClick:
if (this->isHidden())
{
this->show();
}
break;
}
}
void MainWindow::closeEvent(QCloseEvent* event)
{
event->ignore();
hide();
}
void MainWindow::hideEvent(QHideEvent* event)
{
m_pTrayIcon->setIcon(QIcon(":/images/res/stock_3d-texture-spherical_16x16.png"));
}
void MainWindow::showEvent(QShowEvent* event)
{
m_pTrayIcon->setIcon(QIcon(":/images/res/stock_3d-texture-and-shading_16x16.png"));
}

View File

@ -2,21 +2,43 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSystemTrayIcon>
namespace Ui {
class MainWindow;
}
///
// http://qt-project.org/doc/qt-4.8/desktop-systray.html
///
class MainWindow : public QMainWindow
{
Q_OBJECT
typedef QMainWindow BaseType;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void save();
void exit();
void iconActivated(QSystemTrayIcon::ActivationReason reason);
private:
Ui::MainWindow *ui;
QSystemTrayIcon* m_pTrayIcon;
QMenu* m_pTrayIconMenu;
QAction* m_pExitAction;
QPixmap* m_pActiveIcon;
QPixmap* m_pInactiveIcon;
virtual void closeEvent(QCloseEvent* event);
virtual void hideEvent(QHideEvent* event);
virtual void showEvent(QShowEvent* event);
};
#endif // MAINWINDOW_H

View File

@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>FileWatcher</string>
</property>
<property name="windowIcon">
<iconset>