QSystemTrayIcon,打开除mainwindow之外的其他对话框关闭应用程序

时间:2012-07-05 18:54:31

标签: qt indicator tray

正如标题所说,如果我制作一个systemtray图标,可以选择打开其他对话框(例如首选项),当我关闭另一个对话框时,整个应用程序在我打电话时关闭 这>关闭();来自偏好对话框。 以此示例代码为例: main.cpp中:

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/icons/error.png"));
    //replace 'error' with 'video' and recompile. The indicator isn't shown!
    trayIcon->setToolTip("Test");
    QMenu *changer_menu = new QMenu;
    Show_action = new QAction(tr("S&how"),this);
    Show_action->setIconVisibleInMenu(true);
    connect(Show_action, SIGNAL(triggered()), this, SLOT(show_me()));
    changer_menu->addAction(Show_action);
    changer_menu->addSeparator();

    Preferences_action = new QAction(tr("Preferences"), this);
    Preferences_action->setIconVisibleInMenu(true);
    connect(Preferences_action, SIGNAL(triggered()), this, SLOT(showpref()));
    changer_menu->addAction(Preferences_action);

    Quit_action = new QAction(tr("&Quit"), this);
    Quit_action->setIconVisibleInMenu(true);
    connect(Quit_action, SIGNAL(triggered()), this, SLOT(quit_me()));
    changer_menu->addAction(Quit_action);
    trayIcon->setContextMenu(changer_menu);
}

void MainWindow::showpref(){
    pref=new Preferences(this);
    pref->exec();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    trayIcon->show();
    this->hide();
}

void MainWindow::show_me(){
    this->show();
}

void MainWindow::quit_me(){
    this->close();
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSystemTrayIcon>

#include "preferences.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void show_me();
    void quit_me();
    void showpref();

private:
    Ui::MainWindow *ui;
    QSystemTrayIcon *trayIcon;
    QAction *Show_action;
    QAction *Preferences_action;
    QAction *Quit_action;
    Preferences *pref;
};

#endif // MAINWINDOW_H

preferences.cpp:

#include "preferences.h"
#include "ui_preferences.h"

Preferences::Preferences(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Preferences)
{
    ui->setupUi(this);
}

Preferences::~Preferences()
{
    delete ui;
}

void Preferences::on_pushButton_clicked()
{
    /HERE THE WHOLE PROGRAM CLOSES. I WANT ONLY THE PREFERENCES DIALOG TO CLOSE, THE INDICATOR TO STAY
    close();
}

preferences.h:

#ifndef PREFERENCES_H
#define PREFERENCES_H

#include <QDialog>

namespace Ui {
class Preferences;
}

class Preferences : public QDialog
{
    Q_OBJECT

public:
    explicit Preferences(QWidget *parent = 0);
    ~Preferences();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Preferences *ui;
};

#endif // PREFERENCES_H

icons.qrc:

             error.png     

file error.png这里: http://i.imgur.com/beSvX.png

将所有上述文件保存到同一目录并编译为:

qmake -project
qmake *.pro
qmake
make

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

做一点测试,打开主窗口,不要关闭它。打开首选项窗口并关闭它。您的申请不应该以这种方式退出。现在关闭主窗口,应用程序将退出。这是因为QApplication属性“quitOnLastWindowClosed”,默认情况下设置为true。你应该打电话给

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setQuitOnLastWindowClosed(false);
    MainWindow w;
    w.show();

    return a.exec();
}

另请注意,每次要显示首选项时,都会在创建新的首选项实例时从MainWindow泄漏内存。你可以这样做:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    pref(NULL)
{
    // snap, your code goes here
}

void MainWindow::showpref(){
    if( ! pref )
        pref=new Preferences(this);

    pref->exec();
}