문제

As the title says, if I make a systemtray icon which has an option to open an other dialog (e.g. preferences) through there, when I close this other dialog, the whole application closes when I call this>close(); from withing that preferences dialog. Take this example code: 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 here: http://i.imgur.com/beSvX.png

Keep all of the above files to the same dir and compile as:

qmake -project
qmake *.pro
qmake
make

Thanks for any help!

도움이 되었습니까?

해결책

Make a little test, open main window, don't close it. Open preference window and close it. Your application shouldn't quit in this way. Now close the main window and application will quit. This happens because of QApplication property "quitOnLastWindowClosed" which is by default set to true. You should call

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

    return a.exec();
}

Also note that you leak memory from MainWindow while creating new instance of preferences everytime you want to show preferences. You could do something like this:

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top