1

When I run this code and quit the Input Dialog, the app quits (so the system tray icon disapears) :

main.cpp

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

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

    return a.exec();
}

window.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QSystemTrayIcon>

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow();
    ~MainWindow();
    QSystemTrayIcon* sticon;
    QAction* actTexte1;
    QAction* actTexte3;
public slots:
    void changeTexte1();
    void changeTexte3();

};

#endif // MAINWINDOW_H

window.cpp

#include "mainwindow.h"
#include <QSystemTrayIcon>
#include <QWidget>
#include <QMenu>
#include <QtGui>
#include <QApplication>
#include <QInputDialog>

MainWindow::MainWindow()
{
    sticon = new QSystemTrayIcon(this);
    QIcon icon(":/music.png");
    sticon->setIcon(icon);

    QMenu* stmenu = new QMenu(NULL);
    actTexte1 = new QAction("test",NULL);
    actTexte3 = new QAction("exit",NULL);
    stmenu->addAction(actTexte1);
    stmenu->addAction(actTexte3);
    sticon->setContextMenu(stmenu);
    sticon->show();
    connect(actTexte1, SIGNAL(triggered()), this, SLOT(changeTexte1()));
    connect(actTexte3, SIGNAL(triggered()), this, SLOT(changeTexte3()));


}

MainWindow::~MainWindow()
{
}

void MainWindow::changeTexte1()
{
    bool dialogResult;
    QString result = QInputDialog::getText(this, "Echonest API Key", "Enter your API key:", QLineEdit::Normal,
                                           "key", &dialogResult);

}

void MainWindow::changeTexte3()
{
    QApplication::quit();
    exit(1);
}

If in the main I set w.show(); instead of w.hide(); it does not crash BUT an empty window appears and I don't want this, I want an only system tray app. How should I do?

2
  • it's solved with : a.setQuitOnLastWindowClosed(false);
    – AmirH
    Jun 29, 2014 at 14:23
  • can you turn your comment into an answer and mark it as 'accepted' to indicate that this question no longer needs an answer?
    – Erik
    Aug 14, 2014 at 14:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.