Question

Using python and bindings (pyqt, pyopengl) I have created a simple 3D viewer. I would like to create some basic actions operated/triggered by user interaction. The program has 2 parts.

opengl widget:

class OpenGLWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):
        self.parent = parent

        QtOpenGL.QGLWidget.__init__(self, parent)
        ...
    def draw(self):
        #here I would like to change colour of background from right mouse click menu
        glClearColor(self.R,self.G,self.B,1)

main widget:

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.resize(initial_window_width, initial_window_height)
        self.setWindowTitle('Window Name')

        self.setMouseTracking(True)

        #    location of window on screen
        self.setGeometry(5, 25, initial_window_width, initial_window_height)

        self.createActions()
        self.createMenus()        

        #    sets opengl window in central widget position
        self.OpenGLWidget = OpenGLWidget()
        self.setCentralWidget(self.OpenGLWidget)

    @pyqtSlot(QtCore.QPoint)
    def contextMenuRequested(self,point):
        menu = QtGui.QMenu()
        action1 = menu.addAction("Blue")
        self.connect(action1,SIGNAL("triggered()"), self,SLOT("Blue()"))                   
        menu.exec_(self.mapToGlobal(point))

    @pyqtSlot()
    def Blue(self):
        self.R = 0
        self.G = 0
        self.B = 1

The code that runs the entire program:

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    win.setContextMenuPolicy(QtCore.Qt.CustomContextMenu);
    win.connect(win, SIGNAL("customContextMenuRequested(QPoint)"), 
                win, SLOT("contextMenuRequested(QPoint)"))
    win.show() 
    sys.exit(app.exec_())

I would like to know how to change the values R, G, B in main widget that the background colour will change to blue in opengl widget.

Was it helpful?

Solution

Inside OpenGLWidget class add the following method:

def setColor(R, G, B):
    self.R = R
    self.G = G
    self.B = B

Inside MainWindow in Blue() replace the existing code with the following one:

self.OpenGLWidget.setColor(0,0,1)
self.openGLWidget.draw() # or do whatever you want, variables are changed in `OpenGLWidget`

To set color to green, call setColor() with 0,1,0 parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top