[Qt-interest] How to move a pixmap around a scene if I've overridden the paint() method?

Topi Hukkanen topi.hukkanen at gmail.com
Sat Oct 31 11:01:13 CET 2009


Hi,

I have been trying to paint a QPixmap (Finger) on screen and move it along
the x-axis by following the location of the mouse cursor.

The movements were working when I derived my Finger class from
QGraphicsProxyWidget and put inside that was a QPixmap inside a QLabel...
but I was having trouble with large pixmaps over 1000 pixels wide (only ~1/2
of the pixmap would show up).

Now, I tried deriving my Finger class from QGraphicsWidget and overriding
the paint() method.  The large-size pixmaps work just fine, but now the
Finger won't move around the scene, even when the debug output indicates
that it should.

Can anyone give me any pointers?

----------------------------------CODE
START---------------------------------------

#include <QDebug>
#include <QPixmap>
#include <QPainter>
#include <QtPropertyAnimation>

#include "finger.h"

Finger::Finger(QGraphicsItem *parent)
    : QGraphicsWidget(parent)
{
    qDebug() << "Finger::Finger() =>";

    m_xStart = -400;
    m_xOffset = 0;
    m_yOffset = 0;

    m_pixmap = new QPixmap("finger.png");

    qDebug() << "Finger::Finger() <=";
}

int Finger::xOffset()
{
    return m_xOffset;
}

void Finger::setXOffset(int xOffset)
{
    qDebug() << "::setXOffset xOffset >> " << xOffset;
    qDebug() << "::setXOffset comparison >> " << -m_xStart;

/*    if ( xOffset > -m_xStart )
        return;*/

    m_xOffset = xOffset;
    update();
/*
    QTransform trans;
    trans.translate(m_xStart + m_xOffset,m_yOffset);
    setTransform(trans);
*/
}

void Finger::setViewHeight(int height)
{
    m_yOffset = (height - size().height())/2;
    qDebug() << "image height:" << size().height();
    qDebug() << "image width:" << size().width();
    qDebug() << "yOffset:" << m_yOffset;
/*
    QTransform trans;
    trans.translate(m_xStart + m_xOffset,m_yOffset);
    setTransform(trans);
*/
}

void Finger::returnToStartPosition()
{
    QtPropertyAnimation *myAnim = new QtPropertyAnimation(this,
"m_xOffset");
    myAnim->setDuration(300);
    myAnim->setEasingCurve(QtEasingCurve::OutCubic);
    myAnim->setStartValue(m_xOffset);
    myAnim->setEndValue(0);
    myAnim->start();
}

void Finger::paint(QPainter * painter, const QStyleOptionGraphicsItem *
option, QWidget * widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(option);

    qDebug() << "x: " << m_xOffset;

    QTransform trans;
    trans.translate(m_xStart + m_xOffset,m_yOffset);
    painter->setTransform(trans);

    painter->drawPixmap(QPoint(0,0), *m_pixmap);
}


---------------------------------CODE
END--------------------------------------

------------------------OUTPUT START--------------------------

MainWidget::MainWidget() =>
MainScene::MainScene() =>
MainScene::MainScene() <=
Finger::Finger() =>
Finger::Finger() <=
MainWidget::MainWidget() <=
x:  0
x:  0
::setXOffset xOffset >>  1
::setXOffset comparison >>  400
x:  1
::setXOffset xOffset >>  2
::setXOffset comparison >>  400
x:  2
::setXOffset xOffset >>  3
::setXOffset comparison >>  400
x:  3
::setXOffset xOffset >>  4
::setXOffset comparison >>  400
x:  4

............................and so on

-------------------------OUTPUT END--------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091031/28de9c7f/attachment.html 


More information about the Qt-interest-old mailing list