Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Graphics is jumping
Forum Updated to NodeBB v4.3 + New Features

Graphics is jumping

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    twinding
    wrote on last edited by
    #1

    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;;
    

    }

    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      appleorange
      wrote on last edited by
      #2

      ....

      1 Reply Last reply
      0
      • A Offline
        A Offline
        appleorange
        wrote on last edited by
        #3

        .............................

        1 Reply Last reply
        0
        • T Offline
          T Offline
          twinding
          wrote on last edited by
          #4

          Hi all,

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved