我需要在应用程序启动时而不是通过 QPushButton::clicked() 之类的 GUI 事件来初始化和显示 OpenGL 窗口。
我正在尝试在 Android 4.0.4 Samsung Galaxy Tab 2 上运行“openglwindow”Qt 5.1.1 示例。原始项目可以正确构建和运行。我可以看到一个旋转的三角形。
然后我尝试通过事件而不是在 main() 函数中而是在 'CMainWindow::SelectSecondPage()' 插槽中执行 TriangleWindow.show()。Expose 事件成功通过,但应用程序在 initializeOpenGLFunctions() 上的 OpenGLWindow::renderNow() 函数中崩溃。
如何正确初始化和显示 OpenGLWindow?
CMainWindow::CMainWindow(QWidget *parent)
: QMainWindow(parent)
{
QSurfaceFormat format;
format.setSamples(16);
m_pTriangleWindow = new TriangleWindow;
m_pTriangleWindow->setFormat(format);
m_pTriangleWindow->resize(640, 480);
m_pTriangleWindow->setPosition(5000, 5000);
m_pWidgetStack = new QStackedWidget(this);
QWidget* pFirstPage = new QWidget(m_pWidgetStack);
QPushButton* pPushButton = new QPushButton("Start Triangle window", m_pWidgetStack);
QVBoxLayout* pLayout = new QVBoxLayout;
connect(pPushButton, SIGNAL(clicked()), this, SLOT(SelectSecondPage()));
pLayout->addStretch();
pLayout->addWidget(pPushButton);
pLayout->addStretch();
pFirstPage->setLayout(pLayout);
m_pWidgetStack->addWidget(pFirstPage);
m_pSecondPage = new QWidget(m_pWidgetStack);
m_pWidgetStack->addWidget(m_pSecondPage);
setCentralWidget(m_pWidgetStack);
m_pWidgetStack->setCurrentIndex(0);
}
void CMainWindow::SelectSecondPage()
{
m_pWidgetStack->setCurrentIndex(1);
m_pTriangleWindow->show();
m_pTriangleWindow->setAnimating(true);
}
应用输出:
W/Qt (27127): ../src/androidjnimain.cpp:449 (jboolean startQtApplication(JNIEnv*, jobject, jstring, jstring)): Can't set environment ""
W/Qt (27127): kernel/qcoreapplication.cpp:412 (QCoreApplicationPrivate::QCoreApplicationPrivate(int&, char**, uint)): WARNING: QApplication was not created in the main() thread.
D/libEGL (27127): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
D/libEGL (27127): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
D/libEGL (27127): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
W/dalvikvm(27127): dvmFindClassByName rejecting 'org/qtproject/qt5/android/QtNativeInputConnection'
W/dalvikvm(27127): dvmFindClassByName rejecting 'org/qtproject/qt5/android/QtExtractedText'
D/Qt (27127): ../OpenGLWindow/openglwindow.cpp:118 (virtual void OpenGLWindow::exposeEvent(QExposeEvent*)): exposeEvent passed
E/libEGL (27127): eglMakeCurrent:674 error 3009 (EGL_BAD_MATCH)
W/Qt (27127): eglconvenience/qeglplatformcontext.cpp:111 (virtual bool QEGLPlatformContext::makeCurrent(QPlatformSurface*)): QEGLPlatformContext::makeCurrent: eglError: 3009, this: 0x2098190
W/Qt (27127):