sango1994 2022-08-13 09:09 采纳率: 75%
浏览 124
已结题

为什么这段代码会无限触发paintEvent

问题遇到的现象和发生背景

设置了按下pushButton_2触发计时器来重绘,但是实际情况是窗口打开后就一直不停重绘,DEBUG发现窗口构造函数运行后,paintEvent函数不停的循环运行

问题相关代码,请勿粘贴截图

#include "w03_animation2d.h"
#include "ui_w03_animation2d.h"
#include "mainwindow.h"
#include
W03_Animation2D::W03_Animation2D(QWidget *parent) :
QWidget(parent),
ui(new Ui::W03_Animation2D)
{
ui->setupUi(this);
setPalette(QPalette(Qt::white));
m_parent = static_cast<MainWindow*>(parent);
m_state = 0;
m_rotate_angle = m_parent->m_profile_dxf.m_a_cutter;
m_x_axis = m_parent->m_calculate_result.m_x_axis[0];

m_i = 0;
m_length = m_parent->m_calculate_result.m_c_axis.length();

}

W03_Animation2D::~W03_Animation2D()
{
delete ui;
}

void W03_Animation2D::on_pushButton_clicked()
{
close();
}

void W03_Animation2D::paintEvent(QPaintEvent *event)
{
QImage image(size(), QImage::Format_ARGB32_Premultiplied);
QPainter imagePainter(&image);
imagePainter.initFrom(this);
imagePainter.setRenderHint(QPainter::Antialiasing, true);
imagePainter.eraseRect(rect());
generateAnimation(imagePainter);
imagePainter.end();

QPainter widgetPainter(this);
widgetPainter.drawImage(0,0,image);

}
void W03_Animation2D::generateAnimation(QPainter& painter)
{
int i;
int j = 0;
int num_element;
int num_profile = 1;
int type = 0;
int start = 0;
int direction;
int as;
int ae;
int difference;
double xc;
double yc;
double rc;
double xo = 200;
double yo = 200;
double ratio = 2;
painter.setPen(QPen(Qt::black, 1,Qt::SolidLine, Qt::RoundCap));

int length = m_parent->m_profile_dxf.m_profile_info.length();
if(m_i == m_length){
    tim->stop();
    return;
}

m_rotate_angle = m_parent->m_calculate_result.m_c_axis[m_i]+m_parent->m_profile_dxf.m_a_cutter;
m_x_axis = m_parent->m_calculate_result.m_x_axis[m_i];
for (i = 0;i<length;i++) {
    if(num_profile == m_parent->m_profile_dxf.m_profile_selected)
    {
        painter.setPen(QPen(Qt::red, 1,Qt::SolidLine, Qt::RoundCap));
    }
    else {
        painter.setPen(QPen(Qt::black, 1,Qt::SolidLine, Qt::RoundCap));
    }
    if(m_parent->m_profile_dxf.m_profile_info[i] == 0)
    {
        j = 0;
        num_profile++;
    }
    else {
        if(j%2 == 0)
        {
            num_element = m_parent->m_profile_dxf.m_profile_info[i];
            type = m_parent->m_profile_dxf.m_element_info[3*(num_element-1)];
            start = m_parent->m_profile_dxf.m_element_info[3*(num_element-1)+1];
            j++;
        }
        else
        {
            direction = m_parent->m_profile_dxf.m_profile_info[i];
            j++;
            if(direction == 1)
            {
                switch (type) {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    xc = m_parent->m_profile_dxf.m_data[start];
                    yc = m_parent->m_profile_dxf.m_data[start+1];
                    rc = m_parent->m_profile_dxf.m_data[start+2];
                    as = static_cast<int>(m_parent->m_profile_dxf.m_data[start+3])*16;
                    ae = static_cast<int>(m_parent->m_profile_dxf.m_data[start+4])*16;

                    difference = ae-as;
                    if(difference < 0)
                    {
                        difference = difference+360*16;
                    }
                    painter.drawArc(QRectF(ratio*(xc*cos(m_rotate_angle)-yc*sin(m_rotate_angle)-rc)+xo, ratio*(-yc*cos(m_rotate_angle)-xc*sin(m_rotate_angle)-rc)+yo, ratio*2*rc, ratio*2*rc),as+m_rotate_angle*M_1_PI*180*16,difference);
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    xc = m_parent->m_profile_dxf.m_data[start];
                    yc = m_parent->m_profile_dxf.m_data[start+1];
                    rc= m_parent->m_profile_dxf.m_data[start+2];
                    painter.drawEllipse(QPointF(ratio*(xc*cos(m_rotate_angle)-yc*sin(m_rotate_angle))+xo,ratio*(-yc*cos(m_rotate_angle)-xc*sin(m_rotate_angle))+yo),ratio*rc,ratio*rc);
                    break;
                default:
                    ;
                }

            }
            else
            {
                switch (type) {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default:
                    ;
                }
            }
        }
    }

}
painter.drawEllipse(QPointF(ratio*m_x_axis+xo,ratio*0+yo),ratio*m_parent->m_diameter_gringdingwheel/2,ratio*m_parent->m_diameter_gringdingwheel/2);

// m_i++;

}

void W03_Animation2D::on_pushButton_2_clicked()
{
m_i = 0;
tim = new QTimer();

tim->setInterval(20);//20:50帧

connect(tim,SIGNAL(timeout()),this,SLOT(repaint()));
tim->start();

}

  • 写回答

2条回答 默认 最新

  • oldmao_2000 人工智能领域新星创作者 2022-08-13 09:21
    关注

    重绘事件当然是自动刷新的-。-

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月21日
  • 已采纳回答 8月13日
  • 创建了问题 8月13日

悬赏问题

  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码