Vavius
Vavius

Reputation: 218

QGLWidget don't move its contents on scroll

I'm using Qt from python with PySide bindings. The main part of my application is the OpenGL view that can be resized to particular value (this is a simulator for testing my game in different resolutions of mobile devices). I use QGLWidget to render graphics from my game engine and QScrollArea for scrolling.

When I try to scroll GL view nothing happens - it just stays at the same place, but coordinates of QGLWidget are updated just fine which I see through print statements.

Playing around with resizing main window I've came to conclusion that everything inside QGLWidget is snapped to the bottom-left corner of currently visible area. So this would explain why I can't see scrolling.

Am I supposed to update projection matrix manually?

Upvotes: 4

Views: 809

Answers (1)

Fenikso
Fenikso

Reputation: 9451

It sounds like some kind of parenting issue. This works as expected:

import sys
from PySide.QtGui import *
from PySide.QtOpenGL import *
from OpenGL.GL import *
from OpenGL.GLU import *

class GLWidget(QGLWidget):
    def __init__(self, *args, **kwargs):
        QGLWidget.__init__(self,  *args, **kwargs)

    def initializeGL(self):
        glShadeModel(GL_SMOOTH)
        glEnable(GL_DEPTH_TEST)
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClearDepth(1.0)

    def resizeGL(self, w, h):
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glViewport(0, 0, w, h)
        gluPerspective(45.0, w / h, 1, 1000)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()                   
        glTranslatef(0.0, 0.0, -6.0)            
        glBegin(GL_TRIANGLES)                 
        glColor3f(1.0, 0.0, 0.0)           
        glVertex3f(0.0, 1.0, 0.0)        
        glColor3f(0.0, 0.0, 1.0)           
        glVertex3f(-1.0, -1.0, 0.0)        
        glColor3f(0.0, 1.0, 0.0)           
        glVertex3f(1.0, -1.0, 1.0)        
        glEnd()

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        self.scroll = QScrollArea(self)
        self.glWidget = GLWidget(self.scroll)
        self.glWidget.resize(600, 400)
        self.scroll.setWidget(self.glWidget)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.scroll)

        self.setLayout(self.layout)
        self.resize(400, 300)
        self.show()

app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
  • OS: Windows 7 SP1 (32bit)
  • PySide: 1.2.1
  • Qt: 4.8.5
  • PyOpenGL: 3.0.2

Upvotes: 1

Related Questions