QGraphicsView with mouse wrapping
In a document management program that I’m writing I’m displaying images in a QGraphicsView. The QGraphicsView supports a drag mode that allows a user to simply click and drag within the view to pan around, which works great when I a large image displayed and the user has zoomed in quite far.
Panning the image this way does mean that the user has to click back on the image and move the mouse again when the mouse reaches the end of the screen. I needed a way to implement mouse wrapping so that when the mouse reaches the end of the QGraphicsView during panning and the user is still holding down the left mouse button the mouse will jump to the opposite edge, giving a smooth continuous pan.
Here’s what I came up with…
read moreUsing QStyledItemDelegate on a QTableView
Most databases store dates in the format YYYY-MM-DD. When using a QTableView to display a database table in PyQt it’s useful to be able to display this in a different format. Being from the UK I’d rather have the date displayed in the UK style – DD/MM/YYYY. This is where QStyledItemDelegate comes in.
I’d put off looking into this for a while because whenever I tried to look in to using QStyledItemDelegate I saw people mentioning paint methods. I know nothing about painting in PyQt and get quite scared when I see it mentioned. I really need to get over that.
Not to worry though, because it’s not needed…
read moreSplitting old-style JPEG encoded TIFFs
The tiffsplit program from the libtiff library does a good job of splitting multi-page TIFF files into single pages. It doesn’t seem that great at handling TIFFs saved with ‘old-style’ JPEG encoding.
It’ll produce files, and the correct number of files too, but the files are actually in JPEG format. Most image viewers won’t open them still because they’re JPEG files with TIFF headers.
Assuming you have a TIFF file made up entirely of old-style JPEG encoded images you can loop through the files that tiffsplit has exported and remove the first eight bytes to give valid JPEG files.
The Python function below will write the contents of a given file that come after the first eight bytes into a new file with the ‘.jpg’ extension before deleting the original passed file.
1 2 3 4 5 6 7 8 9 10 11 | def remove_header(path): """ Write the contents of the given file after the first 8 bytes to a new file, then delete the original. """ new_path = '%s.jpg' % os.path.splitext(path)[0] with open(path, 'rb') as input_file: with open(new_path, 'wb') as output_file: input_file.seek(8) output_file.write(input_file.read()) os.remove(path) |
Windows 7 Minimal Taskbar Menu
Right clicking the taskbar in Windows 7 and selecting Toolbars and New Toolbar will let you select a folder on your computer to add as a taskbar menu. Here’s how to make the label take up less space…
read moreCleaning QR Codes
At work we use QR Codes. It’s a pretty recent thing. For a few clients we used to scan pages of a file and then have the scanner operator stop what they were doing to give the TIFF file a meaningful filename. Now we have the clients supply cover sheets, generated with custom software, that contain QR codes. These then get processed after scanning and before the scan of the header sheet is discarded the indexing data is decoded using ZBar through an AutoHotkey script.
A few days ago we hit a problem. We’d scanned a few thousand pages but the script was unable to decode any of the indexing data from the QR codes. Not a single page. After looking into it I found that there were quality issues with the printing of the cover sheet. I could tell that the QR code was valid because the app I use on my mobile phone could read it fine, but ZBar could not. And ZBar is what matters – I wasn’t about to sit down decoding QR codes with my phone all day.
read moreInstalling Poppler for PyQt4 on Windows
Poppler is a PDF rendering library based on the xpdf-3.0 code base. It has bindings for Qt4 which we can use through Python/PyQt. Using these libraries under most popular Linux distributions is as simple as installing the Poppler package, but under Windows I’ve found that things are a little more tricky to get working.
At work, with the main business being document scanning, we deal with PDF files a lot. I’ve written a few custom applications that allow people to use the PDF files that we have supplied for them. Some of the applications simply launch the PDF in the user’s default viewer when it is chosen from a list. Others use an embedded version of Adobe’s Acrobat Reader which has to first be installed on the user’s machine.
Acrobat Reader doesn’t play very well with apps written in PyQt in my experience. I don’t really like the idea of having to use an entire program (and Acrobat Reader is a little hefty) embedded in my software just to view a file. Although I was able to load files into the embedded Acrobat Reader and adjust the display preferences I found that the application focus was left with Acrobat Reader after each change. For example, I connected a QSlider to some code to adjust the zoom of the document and all worked fine when using the mouse. Selecting the QSlider with the keyboard and attempting to use it didn’t. Once the zoom had moved one step the focus would be taken from the QSlider and left with Acrobat Reader, meaning that further adjustments from the keyboard required the re-selecting of the QSlider. I tried to find a way around this. I failed.
I went in search of an alternative to Acrobat Reader and found Poppler. Based on the xpdf-3.0 codebase and released under the GPL this seemed to be exactly what I needed. It would let me view PDF files in my PyQt application without having to use Acrobat Reader along with letting me perform a few other PDF related tasks. Great.
I tried to get it to work on Windows. Oh, I tried. I’m not that experienced a programmer and don’t know a great deal about compiling C code. I headed over to the Poppler mailing list and although there were a few people asking how they could use Poppler on Windows there were not that many answers. At least, nothing that I was able to follow. A few people seem to be searching for the term ‘How to compile Poppler on Windows’, but there was nothing really that I could use.
In my Ubuntu Linux installation I was able to use Poppler simply by installing the package. I wrote a quick test program to view a PDF and it worked great and was a lot faster than I expected at rendering the pages. Unfortunately at work I use Windows, as do pretty much all of our customers.
A few months went by with a couple more failed attempts at getting things to work and then last week I actually managed it. It’s cheating a little; I’m using the installer from the KDE Windows Initiative to help me along, but it works and gives me a usable Poppler library that I can use from within PyQt on Windows. This is by no means the only way and there are many projects that use Poppler on Windows quite successfully, but this is a fairly simple way to get things set up and working and I thought I should post a guide on how to do it.
read more