Friday, February 25, 2011

Creating simple in application Help in Qt

Recently I was working on Qt application and I required to create simple in application Help option.

For professional application or complex application you can use Qt Assistant as custom help viewer. Here is example how you can do that.

But for my application, I just wanted to show simple html file and some local resource in html help file.

So I just used QWebView to display html file from Qt's resource file. But I had hard time figuring out how to include image in html file which are in Qt's resource file.

So following code show how you can show html file which show image from Qt's resource file.

You can find my sample project here.

Following code is to display web view and populating it with html data.
MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
   QVBoxLayout* mainLayout = new QVBoxLayout(this);

   QWebView *helpView = new QWebView;
   QFile file(":/help.html");
    if (file.open(QIODevice::ReadOnly))
       helpView->setHtml(file.readAll());

    QPushButton* backBtn = new QPushButton("Exit");
    connect(backBtn,SIGNAL(clicked()),
    QCoreApplication::instance(),SLOT(quit()));

    mainLayout->addWidget( helpView);
    mainLayout->addWidget( backBtn);
}

And following is content of html file which display local image from resource file. We can use "qrc:/" to locate file from resource file.
<html>
<h2> Simple in application help</h2>
<p>
    Sample application that show how simple in application 
    help system can be created using <b>Qt Webkit's QWebView.</b>
    Following image is to demostrate how file or image can 
    be used from Qt's resource file.
</p>

    <img src="qrc:/cloud.png" />
    
<p>
    Hope this will help some one.
</p>
</html>

so this was all, following is snap from my sample application.





Thursday, February 24, 2011

Nokia C7 Qt Ambassador phone

Today I am very happy, I just received my Qt Ambassador kit and it includes New Nokia C7 phone :) . Following are snaps of my C7 Qt Ambassador phone with T-Shirt.




Here is my projects link on Qt Ambassador showcase. And here is link for more information on Qt Ambassador.

Sunday, February 13, 2011

Posting message to facebook wall using iPhone SDK

UPDATE: I have posted my sample application code here. Please visit code fore more information.

Recently I was working on iPhone project that required to post message on Facebook wall.

There is already official facebook API for iOS SDK, which we can use for interacting with Facebook.

You can download Facebook API from GitHub repository using following command.
git clone git://github.com/facebook/facebook-ios-sdk.git

To use downloaded facebook API, you need to bring all facebook API source code under your project. To do this you can just drag it's src folder and drop it under your project in XCode.

Before you start coding, you need a valid facebook application id, you can get one from Developer App. And you need to add this App ID in "fbYOUR_APP_ID" format to your project's .plist file like shown in following snapshot.


Now you are ready to use Facebook iPhone SDK. Main interface class to invoke Facebook API is "Facebook". Following is code to use Facebook class to login and autorize application to use user's facebook account.
facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID"];
   NSArray* permissions =  [NSArray arrayWithObjects:@"read_stream", 
    @"publish_stream", nil];
   [facebook authorize:permissions delegate:self];
With read_stream we can access user's basic account information and with publish_stream we can post to user's wall and make comment.

To authorization to complete successfully we need to implement application:handleOpenURL: method from UIApplicationDelegate and forwad call to facebook API. Like shown in below code.
return [facebook handleOpenURL:url]; 
So everything goes fine then you should be able to retrieve user's account information and post to user's wall as well.

Now to post message to user's wall you can use following code.
NSMutableDictionary* params = [[NSMutableDictionary alloc] 
initWithCapacity:1]
 [params setObject:@"Testing" forKey:@"name"];
 [params setObject:@"IMAGE_URL" forKey:@"picture"];
 [params setObject:@"Description" forKey:@"description"];
 [facebook requestWithGraphPath:@"me/feed" 
        andParams:params 
    andHttpMethod:@"POST" 
      andDelegate:self];
Here "me/feed" is used to post message to logged in user's wall, if you want to post to some other user's wall using logged in user's account then use need to use "USER_ID/feed" instead.

Saturday, February 5, 2011

Playing sound with iPhone SDK

Today I learned to play sound with iPhone SDK, this is first time I worked with sound instead of graphics. So posted my experience here.

In case you need to play short sound in your iPhone App for application action like button click or something similar you can try following code sample.

First of all you need to add Audio Toolbox framework to your project. You can add it like shown in below snap. Right click on Frameworks -> Add -> Existing Frameworks...



After adding audio framework, you can use AudioServices API to play sound. You will need to import AudioToolbox/AudioServices.h file to your header file.

#import <AudioToolbox/AudioServices.h>

Then we need to create system sound id using audio service API like shown in below code.

SystemSoundID soundId;
NSString* soundPath = [[NSBundle mainBundle] pathForResource:@"Sound4" ofType:@"wav"];
CFURLRef soundUrl = (CFURLRef) [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(soundUrl, &soundId);

After creating system sound id, we can use that id to play sound using following code,

AudioServicesPlaySystemSound(soundId);

Now code part is done, you still need to add actual sound file to resources. After adding sound file to resource you can run your application to play sound.

Friday, February 4, 2011

Saving custom structure in NSMutableDictionary/NSMutableArray with iPhone SDK

Recently while working on iPhone code, I required to store my custom structure in to NSMutableDictionary.

Well, NSMutableDictioary only store object derived from NSObject, so either we can make our structure, a class which is derived from NSObject and that's quite heavy and inefficient solution.

Better solution is to store custom structure in NSValue object and save NSValue to NSMutableDictionary.

As apple document suggest, The purpose of NSValue is to allow items of int, float, and char, pointers, structures and object ids data types to be added to collection objects such as instances of NSArray or NSSet, which require their elements to be objects.

Following is simple code that show how custom structure can be store in NSMutableDictionary.
Creating instance of NSMutableDictionary

NSMutableDictionary *map = [[NSMutableDictionary alloc] initWithCapacity:1];


Code to save structure in NSMutableDictionary.

MyStruct myStruct;
myStruct.someInt = 100; myStruct.someBool = NO;
NSValue* valueForStruct = [NSValue valueWithBytes:&myStruct objCType:@encode(MyStruct)];
[map setObject:valueForStruct forKey:@"MyStruct"];


Code to retrieve structure from NSMutableDictionary.

NSValue* valueForStruct = [map objectForKey:@"MyStruct"];
MyStruct myStruct;
[valueForStruct getValue:&myStruct];
NSLog(@"int val:%i, bool val:%i",myStruct.someInt, myStruct.someBool);

Though, it's not that difficult i thought to share this, hope it will help.