使用PyQt6和OpenGL绘制三角形(重写Qt6 C++示例)

问题描述 投票:0回答:1

我当前的 PyQt6 示例不显示窗口,也不给我任何错误消息。这个 PyQt6 示例只是 Qt C++ 的副本。以下 Qt C++ 示例即使在浏览器中和 Android 上也能正常运行:

enter image description here 主.cpp

#include <QtGui/QOpenGLFunctions> #include <QtOpenGL/QOpenGLBuffer> #include <QtOpenGL/QOpenGLShader> #include <QtOpenGL/QOpenGLShaderProgram> #include <QtOpenGLWidgets/QOpenGLWidget> #include <QtWidgets/QApplication> class OpenGLWindow : public QOpenGLWidget, private QOpenGLFunctions { public: OpenGLWindow() { setWindowTitle("OpenGL ES 2.0, Qt6, C++"); resize(350, 350); } void initializeGL() override { initializeOpenGLFunctions(); glClearColor(48.f / 255.f, 56.f / 255.f, 65.f / 255.f, 1.f); QString vertShaderSrc = "attribute vec2 aPosition;\n" "void main()\n" "{\n" " gl_Position = vec4(aPosition, 0.0, 1.0);\n" "}\n"; QString fragShaderSrc = "#ifdef GL_ES\n" "precision mediump float;\n" "#endif\n" "void main()\n" "{\n" " gl_FragColor = vec4(0.2, 0.7, 0.3, 1.0);\n" "}\n"; m_program.create(); m_program.addShaderFromSourceCode(QOpenGLShader::ShaderTypeBit::Vertex, vertShaderSrc); m_program.addShaderFromSourceCode(QOpenGLShader::ShaderTypeBit::Fragment, fragShaderSrc); m_program.link(); m_program.bind(); float vertPositions[] = { -0.5f, -0.5f, 0.5f, -0.5f, 0.f, 0.5f }; m_vertPosBuffer.create(); m_vertPosBuffer.bind(); m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions)); } void paintGL() override { glClear(GL_COLOR_BUFFER_BIT); m_program.bind(); m_vertPosBuffer.bind(); m_program.setAttributeBuffer("aPosition", GL_FLOAT, 0, 2); m_program.enableAttributeArray("aPosition"); glDrawArrays(GL_TRIANGLES, 0, 3); } private: QOpenGLShaderProgram m_program; QOpenGLBuffer m_vertPosBuffer; }; int main(int argc, char *argv[]) { QApplication::setAttribute(Qt::ApplicationAttribute::AA_UseDesktopOpenGL); QApplication app(argc, argv); OpenGLWindow w; w.show(); return app.exec(); }

这是我尝试将上面的代码重写为PyQt6。正如你所看到的,我想将其重写得非常接近 Qt C++。我想要两个非常相似的例子。但这段代码不显示窗口,也不显示任何错误消息。

注意。

我已经解决了下面代码中的问题: 主.py

import sys import numpy as np from OpenGL.GL import (GL_COLOR_BUFFER_BIT, GL_FLOAT, GL_TRIANGLES, glClear, glClearColor, glDrawArrays) from PyQt6.QtCore import Qt from PyQt6.QtOpenGL import QOpenGLBuffer, QOpenGLShader, QOpenGLShaderProgram from PyQt6.QtOpenGLWidgets import QOpenGLWidget from PyQt6.QtWidgets import QApplication class OpenGLWindow(QOpenGLWidget): def __init__(self): super().__init__() self.setWindowTitle("OpenGL ES 2.0, PyQt6, Python") self.resize(350, 350) def initializeGL(self): glClearColor(48 / 255, 56 / 255, 65 / 255, 1) vertShaderSrc = """ attribute vec2 aPosition; void main() { gl_Position = vec4(aPosition, 0.0, 1.0); } """ fragShaderSrc = """ #ifdef GL_ES precision mediump float; #endif void main() { gl_FragColor = vec4(0.2, 0.7, 0.3, 1.0); } """ self.program = QOpenGLShaderProgram(self) self.program.addShaderFromSourceCode(QOpenGLShader.ShaderTypeBit.Vertex, vertShaderSrc) self.program.addShaderFromSourceCode(QOpenGLShader.ShaderTypeBit.Fragment, fragShaderSrc) self.program.link() self.program.bind() vertPositions = np.array([ -0.5, -0.5, 0.5, -0.5, 0, 0.5], dtype=np.float32) self.vertPosBuffer = QOpenGLBuffer() self.vertPosBuffer.create() self.vertPosBuffer.bind() self.vertPosBuffer.allocate(vertPositions, len(vertPositions) * 4) def resizeGL(self, w, h): pass def paintGL(self): glClear(GL_COLOR_BUFFER_BIT) self.program.bind() self.vertPosBuffer.bind() self.program.setAttributeBuffer("aPosition", GL_FLOAT, 0, 2) self.program.enableAttributeArray("aPosition") glDrawArrays(GL_TRIANGLES, 0, 3) if __name__ == "__main__": QApplication.setAttribute(Qt.ApplicationAttribute.AA_UseDesktopOpenGL) app = QApplication(sys.argv) w = OpenGLWindow() w.show() sys.exit(app.exec())

	
python qt opengl pyqt pyqt5
1个回答
2
投票
vertPosBuffer

更改为

self.vertPosBuffer
    

© www.soinside.com 2019 - 2024. All rights reserved.