【问题标题】:QWidget::repaint: Recursive repaint detected using pointersQWidget::repaint:使用指针检测到递归重绘
【发布时间】:2016-12-27 20:13:53
【问题描述】:

我收到以下错误:

QWidget::repaint:检测到递归重绘。

QPainter::begin:一个绘图设备一次只能由一个画家绘制。
QPainter::beginNativePainting:Painter 未激活。
QPainter::setRenderHint:Painter 必须处于活动状态才能设置渲染提示。
QPainter::translate: Painter 未激活。

void Graficador::paintEvent(QPaintEvent *event){

QPainter painter;

painter.begin(this);
painter.beginNativePainting();
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(250, 250);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0.2,0.4,0.3,1);
    glClear(GL_COLOR_BUFFER_BIT);

std::vector<unsigned short> nodosaux;
glPushMatrix();
for (unsigned i=1;i<=automata->Dimension();i++)
 {
   nodosaux=(automata->operator [](i))->Nodos();

   if((automata->operator [](i))->Estado()==0)

       glColor3f(0.3,0.3,0.3);  

               else if((automata->operator [](i))->Estado()==1.0)

                       glColor3f(1.0,1.0,1.0);

                   else if((automata->operator [](i))->Estado()==2.0)

                           glColor3f(0.7,0.7,0.7);

        glBegin(GL_POLYGON);

            for(unsigned j=1;j<=nodosaux.size();j++)
                {
                     glVertex2f(automata->Nodo(nodosaux[j-1]).first,
                                automata->Nodo(nodosaux[j-1]).second);

                }

        glEnd();

        glColor3f(0.0,0.0,0.0);

        glBegin(GL_LINE_LOOP);

        for(unsigned j=1;j<=nodosaux.size();j++)
            {
                 glVertex2f(automata->Nodo(nodosaux[j-1]).first,
                            automata->Nodo(nodosaux[j-1]).second);

            }

        glEnd();
         }


 glPopMatrix();

   glPushMatrix();

   glColor3f(1.0f,1.0f,1.0f);
   glBegin(GL_LINES);

       glVertex2f(-500.0,0.0);
       glVertex2f(500.0,0.0);
       glVertex2f(0.0,-500.0);
       glVertex2f(0.0,500.0);



  glEnd();

   glPopMatrix();

   painter.endNativePainting();

   painter.end();


}

(对不起,使用运算符,我知道它不是正确的使用,但我需要先解决这个问题。

这是 QGLWidget 的paintEvent()。在我开始使用我的类“自动机”的指针之前,我没有任何问题。这个类只有一些带有顶点坐标(x,y)的向量,所以我不知道为什么会出现这个问题。

完整的程序有一个大类 System,里面有:用户界面、自动机和一些用于其他任务的其他类。用户界面里面有这个QGLWidget,我在这里尝试使用的自动机是一个指向系统类中的自动机的指针。

我像这样传递那个指针:

void Cargar_automata(Automata_Celular* ac)
{
 automata = new Automata_Celular();
 automata =ac;
 }

我还有一些其他小部件,但它们只是用于管理文件和计时器的按钮。

【问题讨论】:

  • 您的名为 Cargar_automata 的函数存在内存泄漏。自动机 = new Automata_Celular();不需要,因为自动机指针被下一行的 automata = ac 赋值破坏。

标签: c++ qt pointers qpainter


【解决方案1】:

在 Qt 中使用 OpenGL 小部件时,所有绘画都应在名为 paintGL() 的成员中完成。您正在使用导致您看到的错误消息的paintEvent。

所以你的代码应该是这样的:

void Graficador::paintGL(){

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0.2,0.4,0.3,1);
    glClear(GL_COLOR_BUFFER_BIT);

std::vector<unsigned short> nodosaux;
glPushMatrix();
for (unsigned i=1;i<=automata->Dimension();i++)
 {
   nodosaux=(automata->operator [](i))->Nodos();

   if((automata->operator [](i))->Estado()==0)

       glColor3f(0.3,0.3,0.3);  

               else if((automata->operator [](i))->Estado()==1.0)

                       glColor3f(1.0,1.0,1.0);

                   else if((automata->operator [](i))->Estado()==2.0)

                           glColor3f(0.7,0.7,0.7);

        glBegin(GL_POLYGON);

            for(unsigned j=1;j<=nodosaux.size();j++)
                {
                     glVertex2f(automata->Nodo(nodosaux[j-1]).first,
                                automata->Nodo(nodosaux[j-1]).second);

                }

        glEnd();

        glColor3f(0.0,0.0,0.0);

        glBegin(GL_LINE_LOOP);

        for(unsigned j=1;j<=nodosaux.size();j++)
            {
                 glVertex2f(automata->Nodo(nodosaux[j-1]).first,
                            automata->Nodo(nodosaux[j-1]).second);

            }

        glEnd();
         }


 glPopMatrix();

   glPushMatrix();

   glColor3f(1.0f,1.0f,1.0f);
   glBegin(GL_LINES);

       glVertex2f(-500.0,0.0);
       glVertex2f(500.0,0.0);
       glVertex2f(0.0,-500.0);
       glVertex2f(0.0,500.0);



  glEnd();

   glPopMatrix();
}

【讨论】:

  • 感谢@Peter 的回答。现在几乎所有错误都消失了,但我仍然有错误:“QWidget::repaint: Recursive repaint detected”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2013-12-11
相关资源
最近更新 更多