我有一个选项卡并放置一个从 QGLWidget 继承的对象。OpenGL 工作正常,但不知何故会显示警告:
inconsistent hierarchy, parent: QAccessibleInterface(0x28297c8 name="" role=User
Role obj=QStackedWidget(0x2826b80, name = "qt_tabwidget_stackedwidget") "invisib
le" ) child: QAccessibleInterface(0x26f5d18 name="" role=Client childc=1 obj=QW
idget(0x26ec968, name = "tab") "invisible" )
如果我将我的 GL 对象放在选项卡之外,它不会显示任何警告。所以我怀疑 QTabWidget 和 QGLWidget 之间有问题。顺便说一句,我使用 Qt 5.0.2 和 Visual Studio 2010
这是重现问题的 UI 设计和代码的简单示例
GLImageDisplay.h
#ifndef _GLImageDisplay_H_
#define _GLImageDisplay_H_
#include "stdafx.h"
#include <QtOpenGL/QGLWidget>
class GLImageDisplay : public QGLWidget
{
Q_OBJECT
public:
GLImageDisplay(QWidget *parent = 0);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
};
#endif
GLImageDisplay.cpp
#include "stdafx.h"
#include "GLImageDisplay.h"
#include <GL/glu.h>
#include <GL/glut.h>
GLImageDisplay::GLImageDisplay(QWidget *parent) : QGLWidget (parent)
{
setMouseTracking(true);
}
void GLImageDisplay::initializeGL()
{
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
}
void GLImageDisplay::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glLoadIdentity();
}
void GLImageDisplay::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex2f(100,100);
glVertex2f(100,300);
glVertex2f(300,100);
glEnd();
}
在 setupUi 函数(生成的代码)中,调用 tabWidget->addTab(tab, QString()) 后出现警告;
void setupUi(QMainWindow *QTGLTestClass)
{
if (QTGLTestClass->objectName().isEmpty())
QTGLTestClass->setObjectName(QStringLiteral("QTGLTestClass"));
QTGLTestClass->resize(600, 400);
centralWidget = new QWidget(QTGLTestClass);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
gridLayout_2 = new QGridLayout(centralWidget);
gridLayout_2->setSpacing(6);
gridLayout_2->setContentsMargins(11, 11, 11, 11);
gridLayout_2->setObjectName(QStringLiteral("gridLayout_2"));
tabWidget = new QTabWidget(centralWidget);
tabWidget->setObjectName(QStringLiteral("tabWidget"));
tab = new QWidget();
tab->setObjectName(QStringLiteral("tab"));
gridLayout = new QGridLayout(tab);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(11, 11, 11, 11);
gridLayout->setObjectName(QStringLiteral("gridLayout"));
widget = new GLImageDisplay(tab);
widget->setObjectName(QStringLiteral("widget"));
gridLayout->addWidget(widget, 0, 0, 1, 1);
tabWidget->addTab(tab, QString()); // WARNING APPEARS /////
gridLayout_2->addWidget(tabWidget, 0, 0, 1, 1);
QTGLTestClass->setCentralWidget(centralWidget);
retranslateUi(QTGLTestClass);
tabWidget->setCurrentIndex(0);
QMetaObject::connectSlotsByName(QTGLTestClass);
} // setupUi