September 9, 2011

Ruu_Rinki Ruu_Rinki
Lab Rat
39 posts

[KNotification] method sendEvent() is not called.

Page  
1

Hello.
Maybe somebody knows the answer to my question.
I have a problem.
I need to add to popup notify a few buttons to push on to be performed some action.
Having studied the docks, I realized, that I needed the following methods:
setActions(), setWidget(), sendEvent();
However, upon the occurrence of a particular action, the method sendEvent() does not cause notify into standart KDE “recent notifications”.
Or is there another way to add the button to popup notification.
Can you, please, tell me what’s wrong.

24 replies

September 9, 2011

Tupla Tupla
Lab Rat
39 posts

Have you create a configuration file which specifies the events for your application?

September 10, 2011

Gerolf Gerolf
Robot Herder
3286 posts

Sorry, I did not get the question. What do you want to achieve?
Why do you neet sendEvent?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

September 10, 2011

Tupla Tupla
Lab Rat
39 posts

I think he’s using the class KNotification, from KDE library, which has a method sendEvent() that shows the notification. But it needs a configuration file which defines the events of the application, and the action to perform when one event is triggered (such as popup a widget, display a notification from the system tray, play sound, and so on).

September 12, 2011

Ruu_Rinki Ruu_Rinki
Lab Rat
39 posts

I need to pop-up notification was provided a choice of some action and depending on what action should be chosen should be carried out some piece of code.

If this can be done via the config. –°ould you show how to describe it?

September 12, 2011

Tupla Tupla
Lab Rat
39 posts

If you are using KDE libraries you can refer to the following class documentation:
http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKNotification.html#_details
As you can see, you have to create a notifyrc file and insert there your events which can be fired.
After that you have to create a KNotification object with one of the event name that you specified in that file, and call its sendEvent() after setting up something (such as action list, widget, and signal/slot connections between activated actions and your widget slots).

September 13, 2011

Ruu_Rinki Ruu_Rinki
Lab Rat
39 posts

I can not understand how I relate the parameters in the config file with variables in the application. I have to specify the path to the config? or he has to cling to my program, if I put the config in
/ usr / share / apps / appname / appname.notifyrc?
example from the documentation I have not Campiglio, said that the variables are not defined, but I think that they should be taken from the config.
One more question, in order to establish a widget (ex. pushbutton) to pop notifay, I must declare it as something in the config or should I use the method setWidget ()? If the config – how i can do?
Here is a code, without using the config:

  1. KNotification * notification = new KNotification ("conflict");
  2.  
  3. notification-> setText ("handling conflicts");
  4. notification-> setPixmap (trayIcon-> icon (). pixmap (64,64));
  5. notification-> setActions (QStringList (i18nc ("action: button", "Resume ")));
  6. notification-> setWidget (button);
  7. notification-> sendEvent ();

Tupla, show me, please, how to collaborate with the config.

September 13, 2011

Tupla Tupla
Lab Rat
39 posts

Quoting from the docs

Your application should install a file called: $KDEDIR/share/apps/appname/appname.notifyrc
You can do this with the following CMake command: install( FILES appname.notifyrc DESTINATION ${DATA_INSTALL_DIR}/appname))

So you have to create your *.notifyrc file and put a line in your CMakeLists.txt to install that file (when you give command “make install”).
I don’t understand the variables part, simply create a KNotification object, and in its constructor you pass the eventId, which is the event identifier that you previously specified in your *.notifyrc file.

Step by step:
1) Create a *.notifyrc file, fill it with your global information, context information and events, and add a line to your cmake file, in order to install it.
2) Create a KNotification object when needed, giving as eventId one of the event you specified in your file (i.e., if you wrote [Event/newMail], the eventId is “newMail”).
3) Add an action list to your notification as you did in the previous post.
4) Optionally set a widget which will be bound to the notification, using setWidget().
Beware: the widget associated using setWidget() is not what you may think (not a button for triggering an action, for example).
That widget is simply associated with the notification, and the notification can follow some behaviour (such as “close when the widget is activated”), that’s the widget I’m talking about.
An example is worth a thousand words:

  1. // create notification and associate it with your widget
  2. KNotification * notification = new KNotification ("conflict", yourWidget);
  3.  
  4. notification-> setText ("handling conflicts");
  5. notification-> setPixmap (trayIcon-> icon (). pixmap (64,64));
  6. notification-> setActions (QStringList (i18nc ("action: button", "Resume ")));
  7. // By doing this, when the action of the notification is activated your widget's slot will be called
  8. connect(notification, SIGNAL(activated(unsigned int)), yourWidget, SLOT(handleConflicts()));
  9. notification-> sendEvent ();

Hope now it’s clear ;-)

September 13, 2011

Ruu_Rinki Ruu_Rinki
Lab Rat
39 posts

This is notify.notifyrc file:

  1. [Global]
  2. IconName=Filename
  3. Comment=Friendly Name of app
  4. [Context/group]
  5. Name=Group name
  6. Comment=The name of the group for contacts
  7. [Event/conflict]
  8. Name=Conflict
  9. Comment=You have got a new email
  10. Contexts=group
  11. Action=Popup

After compiling my project, when i’ll try opened the application the system give me following error:
  1. (22998) KIconCache::Private::themeDirsChanged: Theme directory has been modified
  2. (22998) KToolInvocation::klauncher: klauncher not running... launching kdeinit
  3. kdeinit4: preparing to launch /usr/lib/libkdeinit4_klauncher.so
  4. klauncher(23003) kdemain: No DBUS session-bus found. Check if you have started the DBUS server.
  5. kdeinit4: Communication error with launcher. Exiting!
  6. startkdeinitlock(22998): Couldn't start knotify from knotify4.desktop:  "KLauncher could not be reached via D-Bus. Error when calling start_service_by_desktop_path:
  7. Not connected to D-Bus server
  8. "

And i put my code:
  1. int main(int argc, char **argv)
  2. {
  3.   QApplication app(argc, argv);
  4.   QWidget *widget = new QWidget();
  5.   QPushButton *button = new QPushButton("Button:Act", widget);
  6.   button->setEnabled(true);
  7.   KNotification *notification = new KNotification("conflict", widget);
  8.   notification->setText("handling conflicts");
  9.   notification->setActions(QStringList("Resume "));
  10.   notification->setWidget(button);
  11.   notification->sendEvent();
  12. return 0;
  13. }

I did not use method connect() to connect signal / slot.
I think, that it can not be given such an error and besides, it should work without method connect().

Tupla, Can you tell, that I had not right?

September 13, 2011

Tupla Tupla
Lab Rat
39 posts

I see you use QApplication, I don’t know if this is the problem, but I suggest to use KApplication, instead.
Beside that, you have not called app.exec().
Try to call KNotification::event(KNotification::Notification, “yourtext”), to see if standard notification work.

September 13, 2011

Tupla Tupla
Lab Rat
39 posts

It’s a bit tricky to find, but found a solution.
If the notification works with the last instruction that I gave you, but not with your eventId, it’s because you have to kill knotify4 and run kbuildsycoca4 before start your application (obviously your appname.notifyrc must be correctly installed, and everything already set up).

September 14, 2011

Ruu_Rinki Ruu_Rinki
Lab Rat
39 posts

You are right, i forgot app.exec(). Now, standard notification is work.
You are said:
“obviously your appname.notifyrc must be correctly installed, and everything already set up”

I set the appname.notifyrc as root, because when I start to install from a user, I have not access rights.
Where can I find knotify4.desktop and I should just delete it, then start kbuildsycoca4?
How I should correctly installed appname.desktop?

September 15, 2011

Tupla Tupla
Lab Rat
39 posts

For everything to work you have to use KApplication, not QApplication (because the “appname” is given in main using KAboutData class).
Example of main:

  1. KAboutData about("appname", 0, ki18n("MyApp"), "1.0" /* version */, /* for other parameters see KAboutData docs */);
  2. KCmdLineArgs::init(argc, argv, &about);
  3. KApplication app;
  4.  
  5. MyMainWidget *widget = new MyMainWidget;
  6. widget->show();
  7.  
  8. return app.exec();

Obviously, MyMainWidget is your main window class, code it as you like.
If you create a “appname.notifyrc” file, write into it your events and install it with that CMake command, when you send an event in your code, a notification should be displayed.
I didn’t understand why you set your file as root, that command only installs it in your installation prefix.

However, kill knotify4 (CTRL+Esc, search for knotify4 and kill it), then from a terminal run kbuildsycoca4.
If it doesn’t work again, try to copy appname.notifyrc in your main KDE prefix (should be /use/share/kde4/apps if you use kubuntu, for example) by creating a new directory named “appname”, place your file in there. With this extreme way it has to work when you kill knotify4 and run kbuildsycoca4.

September 15, 2011

Ruu_Rinki Ruu_Rinki
Lab Rat
39 posts

It worked, thanks.
Only now I do not understand how it works:)
If I write the line:

  1. KAboutData about ("notify", 0, ki18n ("notify"), "1.0");

It works, despite the fact that my file – it’s psnotify.notifyrc
However, if I write the line:
  1. KAboutData about ("psnotify", 0, ki18n ("psnotify"), "1.0");

It does not cause notice.

I have to you one more question – where information can be read from the config?
And where information can be read, if I deleted all the config files from the system? (Where a thread can sit in the cache)?

September 15, 2011

Tupla Tupla
Lab Rat
39 posts

Mmm, I don’t know, since I tried using the same name for application name in KAboutData and in the name of the notification file.

Sorry, but I don’t understand your question. Maybe you want to know where configuration files for your KDE application are stored and how to access to configuration data?

September 15, 2011

Ruu_Rinki Ruu_Rinki
Lab Rat
39 posts

Yes, exactly :-)
I use Mandriva Linux 2010 and i know that configuration files are in the /usr/local/share/apps/appname/appname.notifyrc
and
/usr/share/apps/appname/appname.notifyrc
Could it be that they are stored in another location?

Page  
1

  ‹‹ QFileDialog does not honor QCoreApplication settings      How to generate a fake mouse events? ››

You must log in to post a reply. Not a member yet? Register here!