![]() ![]() The initializeGL() function is used to perform useful initialization tasks that are needed to render the 3D scene. The setYRotation() and setZRotation() slots perform the same task for rotations measured by the yRot and zRot variables. In the above slot, the xRot variable is updated only if the new angle is different to the old one, the xRotationChanged() signal is emitted to allow other components to be updated, and the widget's updateGL() handler function is called. The widget provides three slots that enable other components in the example to change the orientation of the scene: void GLWidget::setXRotation(int angle) We provide size hint functions to ensure that the widget is shown at a reasonable size: QSize GLWidget::minimumSizeHint() const In this case nothing requires cleaning up. We also implement a destructor to release OpenGL-related resources when the widget is deleted: GLWidget::~GLWidget() : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) The constructor provides default rotation angles for the scene, sets the pointer to the QtLogo object to null, and sets up some colors for later use. This helps to illustrate the differences between subclasses of native widgets (such as QWidget and QFrame) and QGLWidget subclasses. In this example, we split the class into groups of functions and describe them separately. The logo variable will be used to hold a pointer to the QtLogo object which contains all the geometry. ![]() The rest of the class contains utility functions and variables that are used to construct and hold orientation information for the scene. To enable the user to interact directly with the scene using the mouse, we reimplement QWidget::mousePressEvent() and QWidget::mouseMoveEvent(). OpenGL initialization, viewport resizing, and painting are handled by reimplementing the QGLWidget::initializeGL(), QGLWidget::resizeGL(), and QGLWidget::paintGL() handler functions. Void mousePressEvent(QMouseEvent *event) The signals and slots are used to allow other objects to interact with the 3D scene. We use a destructor to ensure that any OpenGL-specific data structures are deleted when the widget is no longer needed (although in this case nothing needs cleaning up). The GLWidget class contains some standard public definitions for the constructor, destructor, sizeHint(), and minimumSizeHint() functions: class GLWidget : public QGLWidget By subclassing this class, and providing reimplementations of event handler functions, 3D scenes can be displayed on widgets that can be placed in layouts, connected to other objects using signals and slots, and manipulated like any other widget. ![]() Qt provides the QGLWidget class to enable OpenGL graphics to be rendered within a standard application user interface. The Hello GL example demonstrates the basic use of the OpenGL-related classes provided with Qt. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |