我必须创建一个简单的框,它根据horizontalSlider / spinBox的值旋转椭圆和一些文本。小部件必须是可调整大小的,并且椭圆的大小必须根据它而改变。
现在只绘制椭圆。如果有效,将添加文本绘画。问题是,如果调整大小后的窗口超过原始窗口大小,则绘画很奇怪。
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是正常的window.show()调用。
答案 0 :(得分:1)
我的猜测是对构造函数的调用会创建一个临时的widget对象并弄乱图形。