Results 1 to 1 of 1

Thread: [SOLVED] Drawing a triangle using PyQt5 and OpenGL (rewriting the Qt C++ example)

  1. #1
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    228
    Thanks
    117
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default [SOLVED] Drawing a triangle using PyQt5 and OpenGL (rewriting the Qt C++ example)

    My current PyQt5 example does not show a window and does not give me any error messages.

    This is Qt C++ example that works very good. I want to use QOpenGLBuffer in my PyQt5 code.

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QOpenGLWidget>
    3. #include <QOpenGLShaderProgram>
    4. #include <QOpenGLBuffer>
    5.  
    6. class Widget : public QOpenGLWidget {
    7. Q_OBJECT
    8. public:
    9. Widget(QWidget *parent = nullptr) : QOpenGLWidget(parent) { }
    10. private:
    11. QOpenGLShaderProgram m_program;
    12. QOpenGLBuffer m_vertPosBuffer;
    13.  
    14. void initializeGL() override {
    15. glClearColor(0.5f, 0.8f, 0.7f, 1.f);
    16. resize(400, 500);
    17. const char *vertShaderSrc =
    18. "attribute vec3 aPosition;"
    19. "void main()"
    20. "{"
    21. " gl_Position = vec4(aPosition, 1.0);"
    22. "}";
    23. const char *fragShaderSrc =
    24. "void main()"
    25. "{"
    26. " gl_FragColor = vec4(0.5, 0.2, 0.9, 1.0);"
    27. "}";
    28. m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSrc);
    29. m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderSrc);
    30. m_program.link();
    31. m_program.bind();
    32. float vertPositions[] = {
    33. -0.5f, -0.5f, 0.f,
    34. 0.5f, -0.5f, 0.f,
    35. 0.f, 0.5f, 0.f
    36. };
    37. m_vertPosBuffer.create();
    38. m_vertPosBuffer.bind();
    39. m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
    40. m_program.bindAttributeLocation("aPosition", 0);
    41. m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3);
    42. m_program.enableAttributeArray(0);
    43. }
    44. void paintGL() override {
    45. glClear(GL_COLOR_BUFFER_BIT);
    46. glDrawArrays(GL_TRIANGLES, 0, 3);
    47. }
    48. void resizeGL(int w, int h) override {
    49. glViewport(0, 0, w, h);
    50. }
    51. };
    52.  
    53. #include "main.moc"
    54.  
    55. int main(int argc, char *argv[]) {
    56. QApplication a(argc, argv);
    57. Widget w;
    58. w.show();
    59. return a.exec();
    60. }
    To copy to clipboard, switch view to plain text mode 

    This is my trying to rewrite the code above to PyQt5. As you can see I want to rewrite it very close to Qt C++. I want to have two very similar example. But this code does not show a windows and does not show any error messages.

    main.py

    Qt Code:
    1. import sys
    2. import numpy as np
    3. from OpenGL import GL as gl
    4. from PyQt5.QtWidgets import QOpenGLWidget, QApplication
    5. from PyQt5.QtGui import (QOpenGLBuffer, QOpenGLShaderProgram,
    6. QOpenGLShader)
    7. from PyQt5.QtCore import Qt
    8.  
    9. class OpenGLWidget(QOpenGLWidget):
    10. def __init__(self):
    11. super().__init__()
    12. self.setWindowTitle("Triangle, PyQt5, OpenGL ES 2.0")
    13. self.resize(300, 300)
    14. def initializeGL(self):
    15. gl.glClearColor(0.5, 0.8, 0.7, 1.0)
    16. vertShaderSrc = """
    17. attribute vec3 aPosition;
    18. void main()
    19. {
    20. gl_Position = vec4(aPosition, 1.0);
    21. }
    22. """
    23. fragShaderSrc = """
    24. void main()
    25. {
    26. gl_FragColor = vec4(0.5, 0.2, 0.9, 1.0);
    27. }
    28. """
    29. program = QOpenGLShaderProgram()
    30. program.addShaderFromSourceCode(QOpenGLShader.Vertex, vertShaderSrc)
    31. program.addShaderFromSourceCode(QOpenGLShader.Fragment, fragShaderSrc)
    32. program.link()
    33. program.bind()
    34. vertPositions = np.array([
    35. -0.5, -0.5, 0.0,
    36. 0.5, -0.5, 0.0,
    37. 0.0, 0.5, 0.0], dtype=np.float32)
    38. vertPosBuffer = QOpenGLBuffer()
    39. vertPosBuffer.create()
    40. vertPosBuffer.bind()
    41. vertPosBuffer.allocate(vertPositions, len(vertPositions) * 4)
    42. program.bindAttributeLocation("aPosition", 0)
    43. program.setAttributeBuffer(0, gl.GL_FLOAT, 0, 3)
    44. program.enableAttributeArray(0)
    45.  
    46. def paintGL(self):
    47. gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    48. gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3)
    49.  
    50. def main():
    51. QApplication.setAttribute(Qt.AA_UseDesktopOpenGL)
    52. a = QApplication(sys.argv)
    53. w = OpenGLWidget()
    54. w.show()
    55. sys.exit(a.exec_())
    56.  
    57. if __name__ == "__main__":
    58. main()
    To copy to clipboard, switch view to plain text mode 


    Added after 1 14 minutes:


    eyllanesc solved my problem here: https://stackoverflow.com/questions/...15511#64915511

    The problem is that the QOpenGLBuffer is a local variable that is destroyed instantly, whereas in C++ QOpenGLBuffer is an attribute of the class. The solution is to change vertPosBuffer to self.vertPosBuffer
    Last edited by 8Observer8; 19th November 2020 at 17:58.

Similar Threads

  1. Replies: 1
    Last Post: 1st August 2016, 18:28
  2. Qt opengl use shader to draw triangle
    By zhangzhisheng in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2015, 16:57
  3. Fail to generate a triangle by openGL(Qt5.0.2)
    By stereoMatching in forum Newbie
    Replies: 5
    Last Post: 23rd June 2013, 13:46
  4. drawing triangle with gradient based on vertices
    By marc2050 in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2011, 13:45
  5. Replies: 2
    Last Post: 30th December 2010, 18:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.