Sunday, July 12, 2009

Hello World in PyQt

Lately I've been looking to python as a quick way to hammer out a qt application. I find it quite nice so I have concocted as small example with my comments. Perhaps this tutorial will help make it sink in for some people. If you have any suggestions or if there is anything you are unclear about or have questions about feel free to ask in the comments section.


I called it "HelloWorld.py", but I have added a few other non-"hello world" related pieces of code to show how they work in PyQt. So without further ado I give to you: "HelloWorld.py"

#first we have to import all of out needed libraries
import sys #this is used for system arguments
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# I could have left out the from and just said import PyQt4
# The main benefit is that I now don't have to type PyQt4.QtGui.QWidget()
# So I feel it improves clarity, you'll want to find what works best for you.



#setting up the application, usually this isn't the structure you would use, however I don't want to get into python classes here.
app = QApplication(sys.argv)

#here I am declaring all the widgets I will need to use later
widget = QWidget()
helloButton = QPushButton("Say Hello World!")
textEdit = QTextEdit()
label = QLabel("Hide me!")
vBoxLayout = QVBoxLayout()

#in this section I am setting up the widget layout
widget.setLayout(vBoxLayout)
vBoxLayout.addWidget(helloButton)
vBoxLayout.addWidget(label)
vBoxLayout.addWidget(textEdit)

#this next line makes it so that when the "-" button is pressed at the start of a line a bulleted list begins
textEdit.setAutoFormatting(QTextEdit.AutoBulletList)

# here I am going declare a python native function, that we will connect to "helloButton"
def sayHelloConsole():
print "Hello World!!!" # this function has changed for latest python. It will become print("Hello World")


#Now I will connect the signals
QObject.connect(helloButton, SIGNAL("clicked()"), sayHelloConsole)
QObject.connect(helloButton, SIGNAL("clicked()"), label, SLOT("hide()"))
#the main differences there is when I had a Qt slot as opposed to a python function to connect to I had to put it in the form SLOT().


#Show the widget & start the application
widget.show()
sys.exit(app.exec_())
I hope this helps someone. Now you can get started learning pyqt/pykde so you can contribute to Kubuntu or what ever your distro of choice may be. If there is enough interest I'll post another article on here using pyKDE libraries as well, but that shouldn't be too difficult.

On the Topic of Meaningless Propaganda.


This is the image that seems to be circulating around on some blogs by people who are trying to say: "I don't have a problem with mono". Instead they chose a meaningless phrase, since no one is actually saying anything about people writing code they are saying people writing in C#/Mono. The main problem I have with this "campaign" is that it fails to address the issue. It's almost as if those who complain about mono are also complaining about other people who code. Which quite frankly isn't true.

If you've read my last post about mono you know I really don't care if people use mono. Now I feel I have gotten it all out of me and I will hopefully resume "meaningful" blogs, I apologize for the brief stint of information free blogs, but I did want to get my two cents in on this issue in particular.

Saturday, July 4, 2009

C#/Mono (to use or not to use)

Well, if I am late to this discussion it's because I've been away from computers for a little while (family illness and so on). I enter this discussion knowing full well it could start a firestorm which is why I will choose my words carefully, so without further ado I begin.


It doesn't matter. Let me clarify:
  1. No major open source desktop project that I know of uses mono by default. In fact both gnome and KDE stick to their "favorite" languages for one reason or another. Virtually all of gnome is C. All non-C based applications are not part of their default setup. I don't know of any major components of KDE that are not C++ or any applications despite plenty of amazing bindings for both environments. So all the warnings about becoming "too dependent" seem a little pre-mature. Esp. considering there has been no plan to switch to mono by any of the major DEs. Now sure, some distributions include mono by default but that's because distributions preferred the mono applications. Does that mean that Ubuntu as a distribution will fall apart if Microsoft decides to attack? No, they'll just replace tomboy with gNote and fspot with gthumb. Which takes me to my next point.
  2. Even if we had some really nice application in mono that far out classed all the non-mono counterparts, and "the hammer fell down" or what have you. The worst that would happen is that it would be ported to java/C++/C and within 6 months if it was that critical of an application. So all this fuss seems a bit pre-mature. The one area where this WOULD become an issue is if mono was used to make ALL of gnome or ALL of KDE. Mostly because running a VM all the time would be a terribly inefficient way to do things. Java and C# are fine for certain applications but not the whole desktop.
  3. I think having mono/C# is excellent for Linux in that it allows us to experiment/learn some of the latest .Net technology. I'd like to think that we run/work on Unix based environments for their technological merits and not because we (apparently) think Microsoft is the devil. I think C# is a pretty cool language. It's far from being my favorite language but it's still fairly interesting. I wouldn't use it on a KDE project for the same reasons I wouldn't use java, they require too much overhead.
  4. One final point, let's all keep in mind that Microsoft is a company that employees a whole lot of developers. Some of those developers work their because they need a pay check, but others are their doing things they are passionate about, just like we are here in KDE. Now obviously KDE is basically all volunteers so the level of passion on the whole is probably higher. What I'm trying to say is: Let's focus on the technology not on the "politics". If your going to reject a tool do so on technical merits. Let's face it, Microsoft, like all corporations, is about making money, so if they attack mono, it's because they think mono is sucking money away from them and I just don't see any of these mono applications as being really that threatening to Microsoft.
  5. Finally, has anyone actually asked Microsoft about this?