从MainWindow(GUI)更改OpenGLWidget中的值

时间:2014-01-09 10:25:36

标签: python opengl pyqt pyopengl

使用python和bindings(pyqt,pyopengl)我创建了一个简单的3D查看器。我想创建一些由用户交互操作/触发的基本操作。该计划有两个部分。

opengl小部件:

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)

主要小部件:

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

运行整个程序的代码:

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_())

我想知道如何更改主窗口小部件中的值R,G,B,背景颜色将在 opengl小部件中更改为蓝色。

1 个答案:

答案 0 :(得分:1)

OpenGLWidget内部添加以下方法:

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

MainWindow中的Blue()内部将现有代码替换为以下代码:

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

要将颜色设置为绿色,请使用setColor()参数调用0,1,0