Graphics is jumping



  • I have a problem with graphics that jumps when I rotate it. If you look at the text when the object is rotating you will see that the number is jumping up and down. What am I doing wrong here? I hope somebody can help on this.

    I have the following source
    @
    #ifndef CH_H
    #define CH_H

    #include <QGraphicsItem>

    class CH : public QGraphicsItem
    {
    public:
    CH(qreal w, qreal h);
    qreal getWidth();
    void advance(int phase);
    protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QRectF boundingRect() const;
    private:
    qreal width;
    qreal height;
    qreal objectRotation;
    };

    #endif // CH_H

    #include "ch.h"
    #include <QPainter>
    #include <QFontMetrics>
    #include <QDebug>

    CH::CH(qreal w, qreal h)
    :
    width(w),
    height(h),
    objectRotation(0)
    {
    }

    void CH::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    //painter->drawRect(-width/2,-height/2,width,height/3);

    painter->setRenderHints(QPainter::Antialiasing |QPainter::TextAntialiasing);
    
    
    // draw a circle
    painter->drawArc(-width/2,-height/2,width,height,0,16*360);
    // draw a smaller circle
    painter->drawArc(-10/2,-10/2,10,10,0,16*360);
    
    // change font
    QFont font("NanumGothic", 8, QFont::Bold);
    painter->setFont(font);
    painter->setBrush(QColor(Qt::black));
    uint rotationStep = 15;
    for(unsigned int i = 0; i < 360; i=i+rotationStep)
    {
        if(((i/rotationStep)%3) == 0)
            font.setPointSize(14);
        else
            font.setPointSize(10);
        QFontMetrics fm(font);
        painter->setFont(font);
        QString degreeString;
        degreeString = QString::number(i/(rotationStep*2));
    
        int pixelsWide = fm.width(degreeString);
        int pixelsHigh = fm.height();
        if((i/rotationStep)%4)
            painter->drawLine(0,-height/2,0, -height/2 + 8);
        else
            painter->drawLine(0,-height/2,0, -height/2 + 14);
    
        if(((i/rotationStep)%4) == 0)
            painter->drawText(-pixelsWide/2,-height/2 + pixelsHigh + 12,degreeString);
    
        painter->rotate(rotationStep);
    }
    painter->translate(0, height/2);
    

    }

    QRectF CH::boundingRect() const
    {
    return QRectF(-width/2,-height/2,width,height);
    }

    void CH::advance(int phase)
    {
    objectRotation +=0.1;
    setRotation(objectRotation);
    }

    qreal CH::getWidth()
    {
    return width;
    }

    #include <QApplication>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsEffect>
    #include <QtGui>
    #include <QPainter>
    #include <QPixmap>
    #include <QScreen>
    #include <QDebug>

    #include "ch.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QGraphicsScene *scene = new QGraphicsScene;
    QGraphicsView *view = new QGraphicsView(scene);
    CH *object = new CH(386,386);
    
    // scene setup
    scene->addItem(object);
    scene->setSceneRect(-view->geometry().width()/2, -view->geometry().height()/2, view->geometry().width(), view->geometry().height());
    
    // view setup
    view->setFrameShape(QFrame::NoFrame);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->show();
    
    // Timer to start rotation
    QTimer *timer = new QTimer;
    QObject::connect(timer, SIGNAL(timeout()), scene, SLOT(advance()));
    timer->start(100);
    
    return a.exec&#40;&#41;;
    

    }

    @



  • ....



  • .............................



  • Hi all,

    Still nobody has answered my question. Is there something wrong with it? Pleeease help. I am new to Qt


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.