Question

/EDIT: solved, see my comment in the 1st answer!/

I am currently building an application which only has a tray icon displayed, so it doesn't have any windows.

Well, in the tray icon I've included a QAction so as to close the application. The thing is, that I get seg fault when I call exit(0); from that function. This is some example code:

//I have a reason for setting it to be a QTimer, please don't even comment on this
class Boot_Timer : public QTimer {
  Q_OBJECT
public:
  explicit Boot_Timer(QObject *parent = 0) : QTimer(parent) {

  }
public Q_SLOTS:
  void set_up_command_line_tray(){
      //Setting up the tray  Icon.
      QSystemTrayIcon *trayIcon_cmd = new QSystemTrayIcon(this);
      trayIcon_cmd->setIcon(QIcon(":/icons/Pictures/myapp.png"));
      trayIcon_cmd->setToolTip("My tray tooltipp");
      QMenu *changer_menu = new QMenu;
      QAction *Quit_action = new QAction(tr("&Quit"), this);
      Quit_action->setIconVisibleInMenu(true);;
      connect(Quit_action, SIGNAL(triggered()), this, SLOT(close_application()));
      changer_menu->addAction(Quit_action);
      trayIcon_cmd->setContextMenu(changer_menu);
      trayIcon_cmd->show();
  }
  void close_application(){
      //HERE I GET SEG FAULT
      exit(0);
  }
    };

Boot_Timer boottimer;
#include "main.moc"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //making some checks (code omitted)
    ...
    boottimer.set_up_command_line_tray()
    return app.exec();
}

So, the tray icon is shown normally and perfectly, but when I choose to Quit the application using the menu I've added to the tray icon, I get a seg fault. I guess that I cannot quit the application using exit(int state) outside main() function and its functions that don't have a parent...

What is the correct way to quit my application, then?

Thanks in advance for any answers!

Was it helpful?

Solution 2

Thanks, that didn't solve it. For some reason, the thing that solved it was to do the following: QSystemTrayIcon *trayIcon_cmd = new QSystemTrayIcon(0); instead of QSystemTrayIcon *trayIcon_cmd = new QSystemTrayIcon(this)

OTHER TIPS

Try to call

qApp->quit(0);

instead of

exit(0);

Remember to #include <QApplication>.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top