Qt QPainter error when rotating an ellipse using horizontalSlider
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 paintin开发者_StackOverflowg 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.
My guess is the call to constructor creates a temporary widget object and messes up the drawing.
精彩评论