Posts tagged 'python'

Install PyQt5 in Python 3 virtual environment

published on February 04, 2015.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

It’s been a while since I last made something with PyQt, so I decided to check out what’s it like nowadays. I’m curious to see what’s new in Qt5 and how does it differ from Qt4. Qt5 also can run under python 3 so I figured to give it a try.

Fedora 21 comes with both python 2.7 and python 3.4, but the default version is 2.7, which means if PyQt5 is installed through the package manager, it will be installed against 2.7. As I’m not currently in the mood of bricking my laptop by changing the default python version, I decided to install PyQt5 in a python virtual environment. Btw, Fedora 22 should have python 3 by default.

In the code samples below, assume the working directory is always ~/pyqt.

Create a virtual environment

First off, let’s create a virtualenv with python 3.4:

virtualenv --python=python3.4 env

Activate the virtualenv and check the python version to verify:

source env/bin/activate
python --version

And that should print something like Python 3.4.1. Leave the virtualenv active, as that’s where PyQt5 is going to be installed.

PyQt5 dependencies

Cool, now with that set up, let’s get PyQt5 dependencies sorted out:

sudo yum install gcc gcc-c++ python3-devel qt5-base qt5-base-devel

As the documentation says, SIP must be installed before PyQt5. Lets grab the sources, configure and make and install them.

wget http://sourceforge.net/projects/pyqt/files/sip/sip-4.16.5/sip-4.16.5.tar.gz
tar xzf sip-4.16.5.tar.gz
cd sip-4.16.5
python configure.py
make
sudo make install
cd ..
rm -r sip-4.16.5*

Not sure why I had to do sudo make install. Verify sip is installed correctly by starting a python shell and typing in the following:

import sip
sip.SIP_VERSION_STR

That should show the sip version 4.16.5.

Installing PyQt5

All the dependencies should be met now, so let’s install PyQt5.

wget http://sourceforge.net/projects/pyqt/files/PyQt5/PyQt-5.4/PyQt-gpl-5.4.tar.gz
tar xzf PyQt-gpl-5.4.tar.gz
cd PyQt-gpl-5.4
python configure.py --qmake /usr/bin/qmake-qt5
make
make install
cd ..
rm -r PyQt-gpl-54*

This will install PyQt5 with the basic modules such as QtCore, QtWidgets and QtSql. Check the output of the python configure.py step to see what modules will be installed. If you need additional modules in your PyQt5 setup, you’ll have to install additional Qt packages on your system. For example, to get the QtWebKit module, install the qt5-qtwebkit package through your package manager first.

Writing a basic PyQt5 app we can verify that it all works. Save the following as pyqt.py:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = QMainWindow()
    window.show()
    sys.exit(app.exec_())

Running it with python pyqt.py should start the application that’s just one small window.

Happy hacking!

Saturday night hack - coords

published on March 24, 2013.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

When I was just starting out learning programming, everything was so simple. I did not care about design patterns and best practices and unit tests and how will users use that piece of code. Hell, I did not even know those things exist. I was having fun, I was learning, I was free to do whatever I wanted to do, I was playing, I was like a child. Not that there is something wrong caring about those things now, but then I was able to put out a piece of code that was fixing a core of one problem I had and that was it. Once I was done with that, I would move on to the next problem. For a long time now I was missing that feeling of not caring, just fix the damn problem and move on. Just to slap together some crappy piece of code, use it once or twice and then forget about it.

And that was exactly what I did last night. I sat down and in some five or six hours I put together coords. It is an ugly as hell little pygtk application, void of any good practices, no tests, just a few comments here and there and that’s it. And I had fun writing it! I completely lost track of time while hacking, got into the zone and today, after some six hours of sleep I woke up feeling like I was on a vacation for a week.

The application itself doesn’t do much, it helps determine coordinates on your desktop. Start the application, click “track”, drag the mouse from the top-left corner you’re interested in to the bottom-left one and that’s it. The entire functionality is shown in this ten second long gif that runs somewhere here on the page. The best part is that it actually solves a problem I had, it helps me determine coordinates on my desktop and then I can use those coordinates for byzanz-record. I loved every second I spent hacking on this.

Best part is that even this little application had a quite an interesting challenge to solve, namely, to determine the position of the mouse anywhere on the screen. It’s no big deal to determine the position of the mouse inside your application, but once you want to break out of it, well, it gets bit tricky.

With pygtk one can only subscribe to events that happen inside the application itself. To go lower than that one needs to use a different library, something like xlib (python-xlib from python). After much poking around I found a way to do it from pygtk itself. It is possible to get hold of the root window instance, which is created by the X server itself (you can’t create a root window from an application, or make an application be a root window, afaik). Once you have the root window, grab the pointer, and then filter events you are interested in on the root window before they get sent from the X server to gtk. Or at least that is how I understood this whole process. While having control over the pointer, get the mouse coordinates from the time left button is pressed till the time it is released. Don’t forget to ungrab/release the pointer once your done. And that’s all there is to it, more or less.

The interesting parts are:

def start_tracking(self, widget, data=None):
    mask = gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK
    self.root_window = gtk.gdk.get_default_root_window()
    gtk.gdk.pointer_grab(self.root_window, False, mask)
    self.root_window.add_filter(self.track_region, self.region)
def track_region(self, event, region):
    x, y, flags = event.window.get_pointer()
    if 'GDK_BUTTON1_MASK' in flags.value_names \
            and region.track_started == False:
        region.start_x = x
        region.start_y = y
        region.track_started = True
        region.track_ended = False
    if 'GDK_BUTTON1_MASK' not in flags.value_names \
            and region.track_started == True:
        region.end_x = x
        region.end_y = y
        region.track_ended = True
        region.track_started = False
        # ungrab the pointer so we get control back
        gtk.gdk.pointer_ungrab()
        self.show_region_values(region)
    return gtk.gdk.FILTER_CONTINUE

Isn’t it ugly? Very. But it works and it solves the problem I had. Btw you can check out the code on github to have a bit more context for all this.

Happy hackin’!

Passing arguments to custom slots in PyQt

published on November 30, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

While hacking on ape, I came to a situation where I need to pass some arguments to a custom defined slot. The slot is being called from different signals, one where the argument is passed by PyQt itself and a second one where I need to programmatically pass the argument to the slot.

First I tried with something like:

action = QAction("My action", parent)
action.triggered.connect(my_slot(my_argument))

which ended in an error: TypeError: connect() slot argument should be a callable or a signal, not ‘NoneType’

After a bit of poking around I passed a lambda function to the connect() method:

action = QAction("My action", parent)
action.triggered.connect(lambda arg=my_argument: my_slot(arg))

Works like a charm.

Also this is my first try to use github gists as a way to embed/highlight code. Hope it’ll work out.

Happy hackin’!

Tags: ape, lambda, pyqt, python, signals, slots.
Categories: Programming.

Connecting signals and slots with PyQt - the new style

published on November 09, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

While working on ape I had a problem with figuring out how to properly connect a signal to a slot, where the signal is emitted by a QTreeView widget. As this is not my first app with python and pyqt, I was doing something like (this is, btw, the “old style”):

self.connect(widget, SIGNAL("emitted_signal()"), self.my_slot)

but it simply didn’t work. Nothing happened. I was trying all different of connect/signal/slot combinations but everything was just dead silent. Google gave only pretty much old posts talking about QT3. Then I figured that, because the QTreeView is “sitting” inside a QDockWidget, maybe that dock widget thingy is somehow intercepting/taking over the signals. Nope. Wth? Wtf is going on? Current pyqt version is (on my machine) 4.6. Last time I used pyqt it was something like 4.2 or 4.3. Something must’ve been changed in the mean time. Off to the pyqt docs I go (btw, I use the official QT docs, the C++ version, there isn’t really a big difference from pyqt): PyQt reference, chapter 7 - “New-style Signal and Slot Support”. A-ha! They changed it! Here is an example of the “new style”:

widget.emmited_signal.connect(self.my_slot)

Oh my, isn’t that just beautiful?! Much more readable and simpler, for me at least. And it works! Yay! The QTreeView signals are happily connected to slots, thus, I’m happy too.

A few paragraphs later, turns out that the “old style” isn’t thrown out, it should still work. Why it didn’t work for me escapes me at the moment, but honestly, I don’t really care as long as the new style is working.

Happy hackin’!

Tags: ape, pyqt, python, signals, slots.
Categories: Development, Programming.

ape is a PHP editor

published on November 06, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

A week ago I started working on a simple editor/IDE for PHP called ape. That’s my weak try on creating a reverse acronym as ape stands for - ape is a PHP editor. This is kind of an introductory post into the whole developing process of it, as my intention is to blog about it a bit more :)

Why?

First, to answer the question everyone is giving me when I mention I’m writing ape:
“Why the hell do you do that (to yourself)?”

Programming is fun. Programming is interesting. Programming makes me learn new things. I like having fun and I do this to learn more about programming and having even more fun. I’m writing web applications each and every day, so writing a desktop app requires a different way of thinking and leaving my “comfort zone” (altho, I’m quite comfortable in front of the keyboard hackin’ away code). ape is written in python and pyqt, but again, it’s not about the language used, for me it is about programming.

The idea

Netbeans is my main IDE for quite some time now and I love it. I know my way around vim, too. But, netbeans has too many features for my taste - I use SVN, git, (on rare occasions I write them) run unit tests from the console. As for vim, maybe I just don’t get it enough, but I feel less productive with it. Debugging PHP apps ends up var_dump-ing things all over the place. So, basically what I want/need from an editor is grouping files into projects, regex search/replace, code coloring & completion and, of course, file editing.

I plan to write a feature a day. On my personal projects I usually want to push out as much code as I can during one day as I’m highly motivated, but this time want to try a different approach. So far I didn’t got far, figured out syntax highlighting, opening files from a file browser widget thingy and things like that, but more on that in other posts.

If anyone wants to take a look, the source code is up on github. It is licensed under GNU GPL v2, as pyqt is licensed under it and I don’t want to waste my time on figuring out could I use MIT or some other license.

Happy hackin’!

Tags: ape, editor, ide, php, pyqt, python.
Categories: Development, Programming.
Robert Basic

Robert Basic

Software engineer, consultant, open source contributor.

Let's work together!

If you require outsourcing or consulting help on your projects, I'm available!

Robert Basic © 2008 — 2019
Get the feed