• May 31, 2024, 10:27:23 pm

Author Topic: [TUT] Use CAknGlobalNote in QML  (Read 1632 times)

0 Members and 1 Guest are viewing this topic.

Offline pisarz1958

  • Developer
  • New Member
  • ****
  • Posts: 26
  • Reputation: 27
  • Oh, I have this little field in my profile!
  • Current Phone: : Nokia 500
[TUT] Use CAknGlobalNote in QML
« on: August 11, 2013, 05:25:08 am »


Hi everyone! In this tutorial I'd like to share an easy way to use CAknGlobalNote right from QML. I know that those things could seem to be rather obvious to experienced developers, but anyway, I decided to share this stuff here. :)

First, we have to modify our .pro file, to load specific Symbian libraries. Just add this stuff there:
Code: [Select]
symbian {
    LIBS += -lavkon \
            -laknnotify \
            -leiksrv
}

Then, create a new C++ class. Make it "QObject" type. I named it simply globalnote, but it's not really important, I guess.


In both header and C++ file we have to include aknGlobalNote.h.
Code: [Select]
#include <aknglobalnote.h>
We have basically empty file, but the includes are ok, so let's strecht your fingers and do some serious coding! Or just copy this stuff, nevermind.  First, we need to convert QString to TDesC, which is used by native Symbian APIs. Then, I've just passed it all to another function, which actually controls displaying the global note. Here it is:

Code: [Select]
void globalnote::displayGlobalNote(QString message)
{
   TPtrC16 aMessage(reinterpret_cast<const TUint16*>(message.utf16()));
   ShowNoteL(aMessage);
}

The "ShowNoteL" function itself is fairly simple. Interfaces with two another functions, to start displaying note and stop after some period of time. I guess I've just copied this stuff from some example on the Wiki, but nevermind. :D

Code: [Select]
void globalnote::ShowNoteL(const TDesC16& aMessage)
{
    ShowGlobalNoteL(EAknGlobalInformationNote, aMessage);
    User::After(800000);
    StopGlobalNoteL();
}

Interesting fact is, that you can replace "EAknGlobalInformationNote"  with loads of other options, like:
  • EAknGlobalWarningNote
  • EAknGlobalConfirmationNote
  • EAknGlobalErrorNote
  • EAknGlobalChargingNote
  • EAknGlobalWaitNote
  • EAknGlobalPermanentNote
  • EAknGlobalNotChargingNote
  • EAknGlobalBatteryFullNote
  • EAknGlobalBatteryLowNote
  • EAknGlobalRechargeBatteryNote
  • EAknCancelGlobalNote
  • EAknGlobalTextNote

And here comes another ancient function for those native GlobalNotes. Creates a new CAknGlobalNote and displays it.

Code: [Select]
void globalnote::ShowGlobalNoteL(TAknGlobalNoteType aNoteType, const TDesC16& aMessage)
{
    iNote = CAknGlobalNote::NewL();
    iNoteId = iNote->ShowNoteL(aNoteType,aMessage);
}

And finally, closing:

Code: [Select]
void globalnote::StopGlobalNoteL(void)
{
    if(iNote && iNoteId >= 0)
    {
        iNote->CancelNoteL(iNoteId);
    }

    iNoteId = -1;
}

So, just copy all this stuff to C++ file. And now, the header file. I guess it doesn't need any explanations, so I'm just simply posting it's contents.

Code: [Select]
#ifndef GLOBALNOTE_H
#define GLOBALNOTE_H

#include <QObject>
#include <aknglobalnote.h>

class globalnote : public QObject
{
    Q_OBJECT
private:
    TInt iNoteId;
    CAknGlobalNote* iNote;
    void StopGlobalNoteL(void);
    void ShowGlobalNoteL(TAknGlobalNoteType aNoteType, const TDesC16& aMessage);
    void ShowNoteL(const TDesC& aMessage);
public:
    explicit globalnote(QObject *parent = 0);
    Q_INVOKABLE void displayGlobalNote(QString message);
signals:
   
public slots:
   
};

#endif // GLOBALNOTE_H

Congratulations! You now have a C++ class, which you can use in Qt for displaying Global Notes. But why stopping here anyway? Let's make it available to QML. Simple, basic stuff. First, let's include our brand-new, shiny class in main.cpp.
Code: [Select]
#include "globalnote.h"And now, in the same file, register it as a QML object.
Code: [Select]
qmlRegisterType<globalnote>("someVeryAwesomeStuff", 1, 0, "GlobalNote");Feel free to replace "someVeryAwesomeStuff" with anything you want. You will use this name later in QML file. Those numbers is version. "GlobalNote" is the name of our new QML object.

Let's go to QML file, for example main.qml. First, we need to import some Very Awesome Stuff.
Code: [Select]
import someVeryAwesomeStuff 1.0(yup, that's why we needed this name and version number before :))

Then, just add this:
Code: [Select]
GlobalNote {
        id: notify
    }

And we can now show a GlobalNote anytime we want. :) This is how it looks like in my app:
Code: [Select]
notify.displayGlobalNote("New message from " + getNameByJid(bareJidLastMsg) + ". You have " + globalUnreadCount + " unread messages.")
Have fun. :)

Offline Allstar12345

  • Allstar Software Founder
  • Administrator
  • Forum Genius
  • ******
  • Posts: 5,235
  • Reputation: 812
    • Allstar Software
  • Current Phone: : OnePlus 8 Pro, Xperia 10, Nexus 6p, Jolla Phone, Nokia N8, Nokia 808 PureView, BlackBerry Z30
Re: [TUT] Use CAknGlobalNote in QML
« Reply #1 on: August 11, 2013, 04:52:07 pm »
Excellent tutorial mate ! +5 rep
I wonder if this way would work with CaknDiscreetPopup ? That would be very very handy and less intrusive to users ;)

Offline huellif

  • Developer
  • Christmas Santa
  • ****
  • Posts: 402
  • Reputation: 212
Re: [TUT] Use CAknGlobalNote in QML
« Reply #2 on: August 11, 2013, 05:19:15 pm »
good tutorial :)

can somebody tell me what's better?
qmlRegisterType<globalnote>....
Or simply creating an object and calling it dirctly?
E.g. onClicked: myQtClass.note("test")
Excellent tutorial mate ! +5 rep
I wonder if this way would work with CaknDiscreetPopup ? That would be very very handy and less intrusive to users ;)

most other popups/notes/other cakn stuff have only tutorials/examples with a resource file, not via calling them directly with dynamic content.

Offline Allstar12345

  • Allstar Software Founder
  • Administrator
  • Forum Genius
  • ******
  • Posts: 5,235
  • Reputation: 812
    • Allstar Software
  • Current Phone: : OnePlus 8 Pro, Xperia 10, Nexus 6p, Jolla Phone, Nokia N8, Nokia 808 PureView, BlackBerry Z30
Re: [TUT] Use CAknGlobalNote in QML
« Reply #3 on: August 11, 2013, 05:23:14 pm »
can somebody tell me what's better?
qmlRegisterType<globalnote>....
Or simply creating an object and calling it dirctly?
E.g. onClicked: myQtClass.note("test")

Whichever floats your boat, both do the same thing i guess

Offline Motaz

  • Developer
  • Forum User
  • ****
  • Posts: 62
  • Reputation: 72
  • Symbian Power User
  • Current Phone: : Nokia 700
Re: [TUT] Use CAknGlobalNote in QML
« Reply #4 on: August 12, 2013, 01:50:14 am »
Thank you for this tutorial ;)

Offline Trong Pham

  • New Member
  • **
  • Posts: 14
  • Reputation: 1
  • Symbian Power User
  • Current Phone: : N95, N82, N8, 1280, C3-01, N9, 808, Lumia 610, ...
Re: [TUT] Use CAknGlobalNote in QML
« Reply #5 on: August 12, 2013, 04:46:04 pm »
thank you, nice tutorial :)