Harsh J

Memoirs of a QWERTY Keyboard

Archive for the ‘Programming’ tag

Basics of PyQt

one comment

Note: If you’re new to using PyQt but are interested in great cross-platform GUI application development please read the PyQt Introduction article.

This is a total beginners-only post. In this post, I will talk about what PyQt is, what it does and what are its major components you should know about before you start thinking about developing with it. I’ve made a simple Google Docs presentation about these and its viewable below or in full screen at this link.

Read the rest of this entry »

Written by Harsh

April 29th, 2009 at 9:34 am

Posted in Software

Tagged with , , , ,

The PyQt Intro

9 comments

This article is an introduction to the PyQt GUI Application Development Framework that uses Python and Nokia’s Qt.

Contents:

  1. Pretext
  2. Prerequisites
  3. Sample Code
  4. Breaking Down the Code
  5. Getting Further

Pretext

Ever wondered how to easily write good programs using Python? Programs that would run on any platform – from Windows to Mac OS X along with the various Linux variants? Programs that look good, look native, and also work great?

Here’s one solution – PyQt! With it you can create complete, working applications that may look like this:

Fresco - An application developed with PyQt4

Nokia’s Qt is a free (LGPL) cross-platform C++/Java based application-framework, with GUI being its most prominent feature.

PyQt is simply a python-binding to the C++ libraries that Qt provides. Using PyQt one can write neat looking GUI applications that do a lot, and do so easily.

Nokia's Qt 4

A series of articles, starting with this one will aim to tutor interested programmers into using this wonderful library, and developing applications with it.

Prerequisites

The most-minimal pre-requisites are:

  • Basic Python programming knowledge.
  • A little know-how about GUI and the event-based-programming concepts.

Two primary resources where we start from:

  1. Nokia’s Qt
  2. Riverbank Computing’s PyQt

Downloading and installing PyQt is fairly easy on Linux thanks to the various package managers. Do check out the download links at each of the above-mentioned links to get them on other platforms as well. Installation help is also provided by Riverbank Computing to help you set-up the PyQt4 library on your OS.

Sample Code

So let’s start off in the same style as almost everything do in a programmer’s perspective: Hello World!

Below is a well documented program for the same:


import sys

# Importing the necessary Qt classes.

from PyQt4.QtGui import QLabel, QApplication

# We use the from foo import * syntax here because
# all of Qt's objects begin with a Q
# and thus we shouldn't run into namespace problems.

if __name__=='__main__':

	App = QApplication(sys.argv)

	# All Qt programs need an
	# QApplication instance.

	# We pass the sys.argv as its arguments
	# because Qt is adept at handling some
	# of the default command-line options
	# like style, size, etc by itself.

	Label = QLabel( "Hello World!" )

	# QLabel is the class providing a
	# simple label

	Label.show()

	# Like in most GUI toolkits, we have
	# to manually set it to show

	App.exec_()

	# Notice the _ after exec, this is to
	# avoid the confusion with Python's
	# exec() built-in-function

	# exec_() starts the main application
	# loop. Something like main() of other
	# toolkits.

This is how it looks when run, a simple application that displays some popular text:

A simple Hello World application written with Qt

Breaking it down

The first thing we have initialized under our ‘__main__’ is the QApplication instance, App. Every GUI application needs this instance to be present. It handles the look and feel of the application and is the main thread of the same. Without it Qt will error out and not run any GUI.

Next we create a label. A label is a simple text-displaying widget used in many forms to describe the input fields and other things. In Qt, its called the QLabel and resides under the QtGui namespace. We create one with a simple Hello World text under the object name Label.

Now we call the show() method of the label so that it appears on the screen. This method is applicable to all QWidget-class children, and QLabel is one among them. Calling show() draws the label widget’s window on the screen.

Finally, we start the application loop, the one responsible for making an application run as an infinite loop waiting for user-interaction. Its done by the exec_() method of QApplication (App is the instance name). Note the _underscore_ in the function name, this is to avoid confusion between Python’s own exec() built-in-function.

Getting Further

Tutorials

FAQs

Read the rest of this entry »

Written by Harsh

April 26th, 2009 at 8:01 am

Posted in Software

Tagged with , , , , , ,

Getting the latest Django Documentation to run offline

4 comments

Yes, really.

Yes, really.

Ok so you have started with Django, the Python web-framework and are finding yourself wasting a lot of precious little seconds on their regularly updated documentation online?

(The docs folder in your downloaded django tarball is just not so up to date!)

Well then this is the guide for you. It will explain in neat little steps on how to go about obtaining the freshest of all django documentation online (From their code repository) and build it for use on your local machine or web-server.

First off, here are the requirements you will need installed before you begin:

  1. SVN (Subversion)
  2. Sphinx (Python)
  3. make, etc… build tools

For example, on a Debian/Ubuntu system you would run:

harsh@work:~$ sudo apt-get install subversion python-sphinx make

Fetch the documentation

Once you have got them all installed, lets start the process by fetching the documentation files from Django’s SVN repository:

harsh@work:~$ mkdir django && cd django

harsh@work:~/django$ svn co http://code.djangoproject.com/svn/django/trunk/docs

After the above command finishes downloading the documentation files (It’s checking-out source code from the repository), you should have a folder named docs under the current working folder (~/django in my example’s case).

Build the HTML documentation

Our next task is to build these reStructuredText (reST) files into HTML using Sphinx. We issue the following command to do so:

harsh@work:~/django$ cd docs

harsh@work:~/django/docs$ make html

Very simple! All the built files would go to the ~/django/docs/_build folder if you’re following my example. This folder would contain two folders named doctree and html. The html one is our sweet nectar of immortality!

Note: There are other make [options] that you could probably use, such as pickle, latex, etc. Also, the file conf.py under the documentation root might be of interest to people who would like to customize things a little more.

Done!

Now that we have our files ready, we can move it to a preferred place. For me it was /var/www (My local web-server root). So all I then had to do was:

harsh@home-desktop:~/django/docs$ mv _build/html /var/www/django-doc

That’s it. Now you can just browse to this folder either directly (Files) or by putting it on your local development server. Enjoy!

P.s. With some more configuration, you can automate this process and even make it update weekly or so for you. So add all the salt you would like for taste, and happy hacking!

Written by Harsh

December 19th, 2008 at 12:52 am

Random

leave a comment

Welcome to yet another random, rand(), whatever :P

First off, music.

3 Doors Down

3 Doors Down

3 Doors Down

This band is pure awesomeness, rock awesomeness specifically!

Though their latest self-titled album had released a few months ago, I only got them this week and every track on it is splendid and not even one makes me skip to the next! The only other album that I’ve enjoyed so much would be Flipsyde’s We The People. Here’s their Last.fm page if you are interested in trying them out.

However, there is always a favorite no matter how much you like all the tracks on an album and mine were It’s Not My Time and She Don’t Want The World.

Their older album, Away from the Sun was also good, with great tracks like When I’m Gone and Here Without You. Nice music!

Didn’t like the even-older album The Better Life though.

Off to Programming.

Ruby

Ruby Programming Language

Ruby Programming Language

I messed around with Ruby a few days. It’s brilliant, and nearly as easy as Python is. But it didn’t fit me so well that I’d shift from Python to it. My prime reason to try it was for seeing how good Ruby On Rails development could be, my interest being sparked by the rave reviews its been getting. It sure is good, web-dev stuff but I’ll go ahead with Django finally.

But doing things in Ruby is sorta easier than in Python. Sort of. But I haven’t poked around much to be sure if it wins over Python or not. Ruby is definitely more Object-Oriented than Python, with every darn thing being an object. With a good editor, ruby files look great and are easy to read as well. Might explore more with it at Project Euler perhaps.

Now for some more KDE4 mixup.

KDE’s Dolphin is a boon for external-storage device users with its neat split screen feature and not to mention the tabbing power. I’d say this is overkill, giving both! And if that were not enough, there’s a button

KDEs Dolphin File Manager

KDE's Dolphin File Manager

that would launch the terminal below the window in another splitter pane. This power feels good to handle, too good. Ironically for me, its philosophy page says otherwise. Ark, the KDE’s default archive-extract/create program is just not good. It fails at basic tasks and stalls while extracting from split RAR files, but thanks to it am more comfortable with the unrar and tar command line programs now.

Am building Amarok 2 (Alpha 2 – 1.86) as I type this now. Will write about it in a later post, if I manage to get it built and running properly. KTouch is another nice application, for improving your touch-typing skills and am addicted to KBattleShip and KHangMan in my free time, for some educative-arcade fun.

Finally, about my life. (Hey who said all above is for people with no life?!)

Not much is happening at college except for some mild interest of mine rising up for IBM’s TGMC 2008, but I most probably won’t be doing any worthwhile thing in it, I don’t like being forced into Java and accompanying technologies from IBM. This Java thing can form another post actually, haha.

Implementing those OS Job-scheduling algorithms in a preemptive manner in C is a nice practice though the syllabus doesn’t clearly require it. And what is this whole Rational Rose thing, I never get it why designing the construct of a software project is easier this way than hard-coding it down from scratch, I find it too confusing drawing diagrams!

Saw some old Sonic the Hedgehog videos thats been doing some spikes lately, and also saw Hancock and The Dark Knight off which the latter was the most awesome movie ever! Its IMDb rating is justified IMO, with Heath Ledger’s death clearly contributing a lot to it.

Thats all for this syscall of random. *Urgh, goes back to the OS Concepts book*

Written by Harsh

August 8th, 2008 at 9:29 am

And thats the second year that just went…

9 comments

I know I rant a lot about my college, the studies that men do, and general stuff in that regard. But underneath all that hate I seem to spew around, I do actually love my course.

It does get tedious when some subject you have no real interest for gets in the way and you have to learn it no matter what but if seen clearly it does teach you something you never knew all your life. And am sure somewhere you would be able to apply the same, if not immediately, in your life.

I shouldn’t be speaking examples here cause cases are all relative to what people study but I’ll push on with it. I had no idea how or what JPEG compression worked or did until this semester. And when I did learn that and GIF, TIFF, MPEG and other stuff like it, I realized how important digital signals were and how much you can achieve by them. I had to remove the mental block that had grown out of my hatred towards any subject that considered a signal x(n) and derived seemingly useless equations out of it a.k.a Digital Signal Processing.

What I mean here is, once I got to know how large the application area of the subject I hated was, I felt shame I did that. But its not entirely my mentality at fault, its also the syllabus outline we have prescribed. The subjects hardly focus on the practical part of a paper, and by practical I do not mean laboratory or stuff like that, but real-world applications. At max all I see is a small 4-5 point block about “Used as”, “Used in” and etc. Never do they include an detailed explanation about at least one of the various interesting applications of the topic.

Not that it would make a difference to the majority who wish to graduate for work purposes alone but to those who actually have joined with an interest it would mean a lot.

Back to normal topics, (i.e. if you’re still reading this far), I’m loving this Gentoo Linux, it didn’t turn out to be hard as predicted at all. Of course I just did the stage3 install via the Handbook but I’ve compiled my own custom kernels and applications and it all seem so easy and nice to do. Well, so much for a normal topic huh?

Gonna watch Iron Man or Speed Racer within the week and both within the next. Just for summer’s sake of course. I really wish The Dark Knight released sooner, its too long the wait since Batman Begins to watch Bale take up the Wayne role again with his perfect straight face, heh.

Anyway, me off now for some stuff I must have done long ago. Got to catch up with the skilled world. :P

Written by Harsh

May 14th, 2008 at 2:18 pm