Bonsoir,

Voila je débute sous Qt et j'ai du mal avec le système de TrayIcone. Mon problème est que sous windows le programme marche très bien mais lorsque je le compile et l'execute sous linux cela ne marche pas j'ai droit à un jolie segmentation fault. Voici le code:

ptray.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef PTRAY_H
#define PTRAY_H
 
#include <QSystemTrayIcon>
#include "pnoteeditor.h"
 
class QAction;
class QMenu;
class QString;
 
class PTray : PNoteEditor
{
      Q_OBJECT
 
      public:
            PTray ();
            void setVisible (bool visible);
 
      protected:
            void closeEvent (QCloseEvent *event);
 
      private slots:
            void iconActivated (QSystemTrayIcon::ActivationReason reason);
            void showMessage ();
            void messageClicked ();
            void newNote ();
            void editNote ();
            void on_noteTitleChanged (const QString &str = "");
            void on_noteTextChanged (const QString &str = "");
            void showConfigDialog ();
            void removeActionFromHistory (QAction *);
            void on_settingsUpdated ();
            void quitTray ();
            void loadRssFile (const QString&);
      private:
            void createActions ();
            void createTrayIcon ();
            void addNoteToHistory (const QString &title = "", const QString &body = "");
            void writeSettings ();
            void writeNotes ();
            void readSettings ();
            QAction *quitAction;
            QAction *minimizeAction;
            QAction *maximizeAction;
            QAction *restoreAction;
            QAction *newNoteAction;
            QAction *currentNoteAction;
            QAction *configAction;
            QActionGroup *history;
            QSystemTrayIcon *trayIcon;
            QMenu *trayIconMenu;
            bool warnOnClose;
            QString notesFileName ;
            QString iconFileName ;
};
 
#endif
ptray.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <QtGui>
#include "ptray.h"
#include "pconfigdialog.h"
#include "pxmlhandler.h"
 
PTray::PTray () : PNoteEditor ()
{
      warnOnClose = false;
      readSettings ();
      createActions ();
      createTrayIcon ();
      connect (trayIcon, SIGNAL (messageClicked ()), this, SLOT (messageClicked ()));
      connect (trayIcon, SIGNAL (activated (QSystemTrayIcon::ActivationReason)), this, SLOT (iconActivated (QSystemTrayIcon::ActivationReason)));
      connect (this, SIGNAL (noteTitleChanged (const QString&)), this, SLOT (on_noteTitleChanged (const QString&)));
      connect(this, SIGNAL (noteTextChanged (const QString&)), this, SLOT (on_noteTextChanged (const QString&)));
      trayIcon->show ();
      setVisible (true);
      setNoteTitle ("");
      setNoteText ("");
      loadRssFile (notesFileName);
}
 
void PTray::loadRssFile (const QString &fileName)
{
      int i;
 
      if(QFile::exists (fileName))
      {
            QFile file (fileName);
            QXmlInputSource inputSource (&file);
            QXmlSimpleReader reader;
            QList<QAction *> list;
            PXmlHandler handler (&list);
            reader.setContentHandler (&handler);
            reader.setErrorHandler (&handler);
            reader.parse (inputSource);
            for(i=0;i<list.count ();i++)
            {
                  QAction *act = list.at (i);
                  addNoteToHistory (act->text (), act->toolTip ());
            }
      }
}
 
void PTray::writeNotes ()
{
      QFile file (notesFileName);
      if (!file.open (QFile::WriteOnly | QFile::Text))
      {
            QMessageBox::warning(this, tr ("Pense bête"),
			    tr ("Ne peut pas écrire de fichier %1:\n%2.")
			    .arg (notesFileName)
			    .arg (file.errorString ()));
      }
      else
      {
            QTextStream out (&file);
            out.setCodec ("UTF-8");
            QApplication::setOverrideCursor (Qt::WaitCursor);
            QApplication::restoreOverrideCursor ();
            out << "<?xml version=\"1.0\"?>\n<rss version=\"2.0\">\n<channel>\n";
            out << "<title>Pense bête</title>\n";
            out << "<link>fichier://" << notesFileName << "</link>\n";
            out << "<description>fichier de données Pense bête.</description>\n";
            out << "<language>" << QLocale::system ().name () << "</language>\n";
            out << "<pubDate>" << QDateTime::currentDateTime ().toString (Qt::TextDate) << "</pubDate>\n";
            out << "<lastBuildDate>" << QDateTime::currentDateTime().toString(Qt::TextDate) << "</lastBuildDate>\n";
            out << "<docs></docs>\n";
            out << "<generator>Pense bête 1.0</generator>\n";
            out << "<managingEditor></managingEditor>\n";
            out << "<webMaster></webMaster>\n";
            out << "<ttl></ttl>\n\n";
            QAction *ita;
            foreach (ita, history->actions ())
            {
                  if (!ita->text ().isEmpty () )
                  {
                        out << "<item>\n<title>" << Qt::escape (ita->text ()) << "</title>\n<link>fichier://" << Qt::escape (notesFileName) << "</link>\n";
                        out << "<description>" << Qt::escape (ita->toolTip ()) << "</description>\n<pubDate>" << Qt::escape (QDateTime::currentDateTime ().toString (Qt::TextDate)) << "</pubDate>\n<guid></guid>\n</item>\n\n";
                  }
            }
            out << "</channel>\n</rss>\n";
      }
}
 
void PTray::writeSettings ()
{
      QSettings settings ("toto", "Pense bête");
      settings.setValue ("pos", pos ());
      settings.setValue ("size", size ());
      settings.setValue ("notesFileName", notesFileName);
      settings.setValue ("iconFileName", iconFileName);
}
 
void PTray::readSettings ()
{
      QSettings settings ("toto", "Pense bête");
      QPoint pos = settings.value ("pos", QPoint (200, 200)).toPoint ();
      QSize size = settings.value ("size", QSize (800, 600)).toSize ();
      notesFileName = settings.value ("notesFileName","desktop_notes.rss").toString ();
      iconFileName = settings.value ("iconFileName",":/images/burn.png").toString ();
      setWindowIcon (QIcon (iconFileName));
      resize (size);
      move (pos);
}
 
void PTray::setVisible (bool visible)
{
      minimizeAction->setEnabled (visible);
      maximizeAction->setEnabled (!isMaximized ());
      restoreAction->setEnabled (isMaximized () || !visible);
      PNoteEditor::setVisible (visible);
}
 
void PTray::closeEvent (QCloseEvent *event)
{
      if (trayIcon->isVisible () && !warnOnClose)
      {
            QMessageBox::information (this, tr ("Pense bête"),
                                 tr ("Le programme continuera de fonctionner dans la "
                                     "barre des tâches. Pour quitter le programme, "
                                     "choisissez <b>Quitter</b> dans le menu contectuel "
                                     "d'entré de la barre des tâches."));
            warnOnClose = true;
            hide ();
            event->ignore ();
      }
      else if (warnOnClose)
      {
            hide ();
            event->ignore ();
      }
}
 
void PTray::iconActivated (QSystemTrayIcon::ActivationReason reason)
{
      switch (reason)
      {
            case QSystemTrayIcon::Trigger:
                  if (isVisible ())
                  {
                        setVisible (false);
                  }
                  else
                  {
                        showNormal ();
                  }
                  break;
            case QSystemTrayIcon::DoubleClick:
                  break;
            case QSystemTrayIcon::MiddleClick:
                  showMessage ();
                  break;
            default:
                  ;
      }
}
 
void PTray::showConfigDialog ()
{
      setVisible (true);
      PConfigDialog *config = new PConfigDialog (this);
      config->setMinimumSize (config->size ());
      config->setMaximumSize (config->size ());
      connect (config, SIGNAL (noteRemoved (QAction *)),this, SLOT (removeActionFromHistory (QAction *)));
      connect (config, SIGNAL (settingsUpdated ()),this, SLOT (on_settingsUpdated ()));
      config->setNoteList (history);
      config->show ();
}
 
void PTray::on_settingsUpdated ()
{
      readSettings ();
      trayIcon->setIcon (QIcon (iconFileName));
}
 
void PTray::removeActionFromHistory (QAction *action)
{
      if (action != currentNoteAction)
      {
            history->removeAction (action);
            trayIconMenu->removeAction (action);
      }
}
 
void PTray::showMessage ()
{
      trayIcon->showMessage (noteTitle (), noteText (), QSystemTrayIcon::Information,15 * 1000);
}
 
void PTray::messageClicked ()
{
      showNormal ();
}
 
void PTray::on_noteTitleChanged (const QString &str)
{
      QAction *ita = history->checkedAction ();
      ita->setText (str);
}
 
void PTray::on_noteTextChanged (const QString &str)
{
      QAction *ita = history->checkedAction ();
      ita->setToolTip (str);
}
 
void PTray::addNoteToHistory (const QString &title, const QString &body)
{
      QAction *oldNote = new QAction (title, this);
      oldNote->setToolTip (body);
      oldNote->setCheckable (true);
      oldNote->setChecked (false);
      connect (oldNote, SIGNAL (triggered ()), this, SLOT (editNote ()));
      trayIconMenu->addAction (oldNote);
      history->addAction (oldNote);
}
 
void PTray::newNote ()
{
      setVisible (true);
      addNoteToHistory (noteTitle (), noteText ());
      setNoteTitle ("");
      setNoteText ("");
      writeNotes ();
}
 
void PTray::editNote ()
{
      QAction *action = qobject_cast<QAction*> (sender ());
      if (action)
      {
            setNoteTitle (action->text ());
            setNoteText (action->toolTip ());
      }
}
 
void PTray::quitTray ()
{
      writeNotes ();
      writeSettings ();
      qApp->quit ();
}
 
void PTray::createActions ()
{
      minimizeAction = new QAction (tr ("Réduire la fenêtre"), this);
      minimizeAction->setIcon (QIcon (":/images/minimize.png"));
      connect (minimizeAction, SIGNAL (triggered ()), this, SLOT (hide ()));
      maximizeAction = new QAction (tr ("Agrandir la fenêtre"), this);
      maximizeAction->setIcon (QIcon (":/images/maximize.png"));
      connect (maximizeAction, SIGNAL (triggered ()), this, SLOT (showMaximized ()));
      restoreAction = new QAction(tr("Restaurer"), this);
      restoreAction->setIcon (QIcon (":/images/restaure.png"));
      connect (restoreAction, SIGNAL (triggered ()), this, SLOT (showNormal ()));
      quitAction = new QAction (tr ("Quitter"), this);
      quitAction->setIcon (QIcon (":/images/exit.png"));
      connect (quitAction, SIGNAL (triggered ()), this, SLOT (quitTray ()));
      newNoteAction = new QAction (tr ("Nouvelle note"), this);
      newNoteAction->setIcon (QIcon (":/images/list_add.png"));
      connect (newNoteAction , SIGNAL (triggered ()), this, SLOT (newNote ()));
      currentNoteAction = new QAction (tr ("Note en cours"), this);
      connect (currentNoteAction , SIGNAL (triggered ()), this, SLOT (editNote ()));
      configAction = new QAction (tr ("Configurer"), this);
      configAction->setIcon (QIcon (":/images/configure.png"));
      connect (configAction, SIGNAL (triggered ()), this, SLOT (showConfigDialog ()));
      history = new QActionGroup (this);
      history->addAction (currentNoteAction);
      currentNoteAction->setCheckable (true);
      currentNoteAction->setChecked (true);
}
 
void PTray::createTrayIcon ()
{
      trayIconMenu = new QMenu (this);
      trayIconMenu->addAction (minimizeAction);
      trayIconMenu->addAction (maximizeAction);
      trayIconMenu->addAction (restoreAction);
      trayIconMenu->addSeparator ();
      trayIconMenu->addAction (configAction);
      trayIconMenu->addAction (newNoteAction);
      trayIconMenu->addAction (currentNoteAction);
      trayIconMenu->addSeparator ();
      trayIconMenu->addAction (quitAction);
      trayIcon = new QSystemTrayIcon (this);
      trayIconMenu->addSeparator ();
      trayIcon->setContextMenu (trayIconMenu);
      trayIcon->setIcon (QIcon (iconFileName));
}
main.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <QtGui>
#include "ptray.h"
 
int main (int argc, char **argv)
{
      QApplication app(argc, argv);
      if (!QSystemTrayIcon::isSystemTrayAvailable ())
      {
            QMessageBox::critical (0, QObject::tr ("Pense bête"),
                              QObject::tr ("Aucune barre des tâches n'a été détecté "
                                          "sur ce système."));
 
            return 1;
      }
      PTray window;
 
      return app.exec();
}
Pour information je l'ai passé sous gdb et il me dit que l'erreur survient a la ligne:

Dans le main. Voici le rapport:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Program terminated with signal 11, Segmentation fault.
[New process 9830]
#0  0xb78df368 in ?? () from /usr/lib/libQtGui.so.4
(gdb) b main
Breakpoint 1 at 0x804f3c5: file main.cpp, line 6.
(gdb) r
Starting program: /home/...... 
[Thread debugging using libthread_db enabled]
[New Thread 0xb70beb60 (LWP 9843)]
[Switching to Thread 0xb70beb60 (LWP 9843)]
 
Breakpoint 1, main (argc=1, argv=0xbf85ee64) at main.cpp:6
6       QApplication app(argc, argv);
(gdb) n
Qt: gdb: -nograb added to command-line options.
Use the -dograb option to enforce grabbing.
7       if (!QSystemTrayIcon::isSystemTrayAvailable ())
(gdb) n
15       PTray window;
(gdb) n
 
Program received signal SIGSEGV, Segmentation fault.
0xb797e368 in ?? () from /usr/lib/libQtGui.so.4
Merci d'avance