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

hyt*_*omo 3 qt tray indicator

正如标题所说,如果我做这有一个选项经过那里打开一个对话框等(例如偏好)一个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();
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

icons.qrc:

error.png

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

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

qmake -project
qmake *.pro
qmake
make
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

Kam*_*mek 5

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

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

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

另请注意,每次要显示首选项时,都会在创建新的首选项实例时从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();
}
Run Code Online (Sandbox Code Playgroud)