我收到以下错误:
QWidget::repaint: Recursive repaint detected.
QPainter::begin: A paint device can only be painted by one painter at a` time.
QPainter::beginNativePainting: Painter not active.
QPainter::setRenderHint: Painter must be active to set rendering hints.
QPainter::translate: Painter not active.
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()。在我开始使用我的类“automata”的指针之前,我没有遇到任何问题。这个类只有一些顶点坐标为 (x,y) 的 vector ,所以我不知道为什么会出现这个问题。
完整的程序有一个大类系统,里面有:一个用户界面,一个自动机和一些用于其他任务的其他类。用户界面里面有这个QGLWidget,我在这里尝试使用的自动机是一个指向系统类中自动机的指针。
我像这样传递指针:
void Cargar_automata(Automata_Celular* ac)
{
automata = new Automata_Celular();
automata =ac;
}
我还有一些其他小部件,但它们只是管理文件和计时器的按钮。
最佳答案
在 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();
}
关于c++ - QWidget::repaint:使用指针检测到递归重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059848/