2
votes

I have to create a simple box which rotates an ellipse and some text depending upon value from horizontalSlider/spinBox. The widget has to be resizable, And size of the ellipse has to change depending upon that.

For now only the ellipse is being painted. The text painting will be added if this works. The problem is that if the window after resize exceeds the original window size, the painting is weird.

window.h:

#ifndef WINDOW_H
#define WINDOW_H

#include <QtGui>
#include "ui_form.h"

class Window : public QWidget, private Ui::Form
{
    Q_OBJECT

public:
    Window(QWidget *parent = 0);

public slots:
    void rotateEllip(int angle);
    void rotateText(int angle);

protected:
    void paintEvent(QPaintEvent *event);
};

#endif // WINDOW_H

window.cpp:

#include "window.h"

qreal textAngle = 0.0;

qreal ellipAngle = 0.0;

Window::Window(QWidget *parent) : QWidget(parent)
{
    setupUi(this);

    connect(spinBox_ellipse,SIGNAL(valueChanged(int)),this,SLOT(rotateEllip(int)));
    connect(horizontalSlider_ellipse,SIGNAL(valueChanged(int)),this,SLOT(rotateEllip(int)));
    connect(spinBox_text,SIGNAL(valueChanged(int)),this,SLOT(rotateText(int)));
    connect(horizontalSlider_text,SIGNAL(valueChanged(int)),this,SLOT(rotateText(int)));
}

void Window::rotateEllip(int angle)
{
    ellipAngle = (qreal) angle;
    Window::Window(this);
}

void Window::rotateText(int angle) 
{
    textAngle = (qreal) angle;
    Window::Window(this);
}

void Window::paintEvent(QPaintEvent *event) 
{
    QPen pen(Qt::black,2,Qt::SolidLine);
    QPoint center(0,0);

    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing);
/*    Drawing ellipse*/


    painter.eraseRect(10,10,frame_ellipse->width(),frame_ellipse->height());
    painter.translate(frame_ellipse->width()/2+10,frame_ellipse->height()/2+10);
    painter.rotate(ellipAngle);
    if (frame_ellipse->width() > frame_ellipse->height()) painter.drawEllipse(center,(frame_ellipse->height()/4)-5,(frame_ellipse->height()/2)-10);
    else if (frame_ellipse->width() <= frame_ellipse->height() ) painter.drawEllipse(center,(frame_ellipse->width()/2)-10,(frame_ellipse->width()/4)-5);
    painter.rotate(-ellipAngle);
    painter.translate(-frame_ellipse->width()/2+10,-frame_ellipse->height()/2+10);
}

main.cpp is normal window.show() calling.

1
Please format the code correctly. Highlight the code and press Ctrl+K. More info -> stackoverflow.com/editing-helpJujjuru
Done thank you. I did not know earlier as I hadn't read it.Khushman Patel
Why do you call the constructor in your rotate slots? If you want to repaint the widget, call update().Stephen Chu
Okay. The problem was also solved. Thank you Stephen Chu.Khushman Patel

1 Answers

1
votes

My guess is the call to constructor creates a temporary widget object and messes up the drawing.