How do you create a custom QCursor in Python Qt?

Steegg at gmail.com Steegg at gmail.com
Fri Dec 2 10:31:00 EST 2005


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




More information about the Python-list mailing list