How do you create a custom QCursor in Python Qt?

S

Steegg

I am a newcomer to using Python and Qt and the main problem that I have
is the dearth of any example code or books describing the use of Python
and Qt together.

My current problem is that I want to create a custom cursor, from my
understanding of it I need to create two "QBitmap"s, one of which will
be the mask and then using these to create a "QCursor".
I don't mine whether the "QBitmap"s are hardcoded in the source code or
come from image files, just as long as I can create a new cursor:

I have searched all the available code sources I can find:
www.koders.com
Eric
Veus
Civil
Qomics
for examples of how to create cursors but to no avail. So if amyone can
tell me what I'm doing wrong, it would be much appreciated, here's my
code:


#
# InlineBitmapCursor.py
# environment: Mac OS X 10.4.2 - Python 2.4.1 - Qt 3.3.5
# status: does not produce any error messages but also does not
change
# the cursor.
# standalone file

import sys, os
from qt import *

class MainWindow(QMainWindow):

def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.setCaption("Custom Cursor")
self.grid=QGrid(2, self)
self.setCentralWidget(self.grid)

self.grid.setFrameShape(QFrame.StyledPanel)

self.button1=QPushButton("Arrow Cursor", self.grid)

self.button2=QPushButton("Bitmap Cursor", self.grid)

self.arrowCursor = QCursor(Qt.ArrowCursor)
self.setCursor(self.arrowCursor)

self.connect(self.button1, SIGNAL("clicked()"), self.arrow)
self.connect(self.button2, SIGNAL("clicked()"), self.bitmap)

# 8 * 8 bitmap - two random bit maps
# self.bitmap2 = QBitmap(8, 8,
"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a")
self.bitmap2 = QBitmap(8, 8,
"\x89\x55\x55\x55\x0d\x0a\x1a\x0a")
self.bitmap3 = QBitmap(8, 8,
"\x88\x67\x34\xaa\x23\x60\x84\xbb")
print "bitmap2 " + repr(self.bitmap2)
print "bitmap3 " + repr(self.bitmap3)

self.inlineBitmapCursor = QCursor(self.bitmap2, self.bitmap3)
print "before setCursor"
self.setCursor(self.inlineBitmapCursor)
print "after setCursor"



def arrow(self):
self.setCursor(self.arrowCursor)


def bitmap(self):
self.setCursor(self.inlineBitmapCursor)


def main(args):
app=QApplication(args)
win=MainWindow()
win.show()
app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()")
)
app.exec_loop()


if __name__=="__main__":
main(sys.argv)




This is what happen when it executes:

[~] steve% "/usr/local/bin/pythonw"
"/Users/steve/PythonDev/qt3.3.5/CursorExamples/InlineBitmapCursor.py"
&& echo Exit status: $? && exit 1
bitmap2 <qt.QBitmap object at 0x80b30>
bitmap3 <qt.QBitmap object at 0x80b70>
before setCursor
after setCursor


thanks,
Steve Greenwood
 
P

Phil Thompson

I am a newcomer to using Python and Qt and the main problem that I have
is the dearth of any example code or books describing the use of Python
and Qt together.

My current problem is that I want to create a custom cursor, from my
understanding of it I need to create two "QBitmap"s, one of which will
be the mask and then using these to create a "QCursor".
I don't mine whether the "QBitmap"s are hardcoded in the source code or
come from image files, just as long as I can create a new cursor:

I have searched all the available code sources I can find:
www.koders.com
Eric
Veus
Civil
Qomics
for examples of how to create cursors but to no avail. So if amyone can
tell me what I'm doing wrong, it would be much appreciated, here's my
code:

How about looking at the cursor.py example that comes with PyQt? It
demonstrates the standard cursors and implements a custom cursor.

Phil
 
S

Steegg

Thanks Phil,

Good idea, I had failed to notice these code examples and been
struggling the "qt3" examples from pyqtsrc.tgz for months.

So, I found my mistake, I was using a 8*8 bitmap, I have now corrected
it and use a 16*16 bitmap (which is the standard size on Mac OS X) read
in from a "PNG" file created in Photoshop in bitmap mode and it works
just great.

I failed to read the C++ documentation for Qt closely enough where it
states:

Valid cursor sizes depend on the display hardware (or the underlying
window system). We recommend using 32x32 cursors, because this size is
supported on all platforms. Some platforms also support 16x16, 48x48
and 64x64 cursors.

As a Python novice I had simply taken the bitmap that was available in
the documentation for Qbitmap, which happens to be an 8*8 bitmap.


Interestingly on my G5 iMac Mac OS X 10.4.3 running Python 2.4.1 &
Qt3.3.5 QCursors of both 16*16 & 32*32 works perfectly; whereas on my
G4 Powerbook Mac OS X 10.4.2 running the same Python 2.4.1 & Qt3.3.5, a
16*16 QCursor works but a 32*32 QCursor doesn't, but since there are no
error messages that is all that I can report. Interesting!

I have also tested my code on a PC, which supports 32*32 but does not
support
16*16.

-------------------

I have two auxiliary questions if anybody can help me.

1: When I click on a button the cursor does not change immediately, the
cursor only actually gets re-drawn when I physically move the mouse.
Is there a way to get it to update instantaneously?
After "setCursor() I call "repaint(True)" but this does not have the
desired effect.

def bitmap(self):
self.setCursor(self.inlineBitmapCursor)
self.repaint(True)


2: The cursor that I want to customise is the wait cursor; under Mac OS
X this is an animated cursor displaying a wristwatch with hands that go
around?
So my question is where do I find the code that produces this effect,
is it written in C++ or Python and how can I imitate it?

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top