Qt World Summit: Submit your Presentation

How can I do pixel image drawing with Python and QML?

  • I am sorry if I do a double post with the MeeGo forum, but up to now I haven't got a helpful answer. So I hope I can get some more help here.

    For Maemo 5 I wrote some applications which do pixel drawing with a QPainter.
    I am now wondering how I could port this to QML.

    I searched the web quite a while, but couldnt find any examples how to paint from Python on a QML page.

    On PyQt I did it like this:
    class MyCanvas(QtGui.QWidget):
    def init(self, parent=None):
    QtGui.QWidget.init(self, xxx)
    self.setGeometry(10, 180, 781, 241)

    def paintEvent(self, event):
    paint = QtGui.QPainter()

    canvas = MyCanvas()

    This then draw directly on my QWidget.

    So my question is, how would I be able to do something like this with QML?
    I know there are some C++ examples, but I do not understand how to adapt them to Python :(
    Edit/Delete Message

  • Hi take a look on this example write using PySide: https://qt.gitorious.org/pyside/pyside-examples/blobs/HEAD/examples/declarative/extending/chapter1-basics/basics.py

    I know this is possible using PySide I do not know if is possible to do thin on PyQt.

  • Thank you very much!
    This was exactly what I was looking for.

    I am using PySide, so I don't bother PyQt. How ever they are mostly compatible, so it might work there as well.

  • I got it working quite well, how ever now I am facing a new problem:
    In python, I have a timer which should redraw my graph every 1 seconds. How ever, how do I force the update?
    Before I could do it in this way:
    update() then would somehow call the paintEvent function in the canvas object.

    But now I register the QML object with
    @qmlRegisterType(Graph, 'myAngleGraph', 1, 0, 'AngleGraph')@
    so there is no object I could call within Python.

    I guess there would also be a way of having a timer in QML which reloads the graph, but I would prefer to do it from Python so I only redraw it when necessary.

  • I found now a solution. It might not be the nicest one, but it works.

    In python, I call a function in QML, and from there I move the graph around, just one pixel right and left again. this then forces a redraw.

    For more infos about it, have a look here: http://forum.meego.com/showthread.php?t=4412

Log in to reply