【问题标题】:Change value in OpenGLWidget from MainWindow (GUI)从 MainWindow (GUI) 更改 OpenGLWidget 中的值
【发布时间】:2014-01-27 20:17:27
【问题描述】:

使用 python 和绑定(pyqt、pyopengl)我创建了一个简单的 3D 查看器。我想创建一些由用户交互操作/触发的基本操作。该程序有 2 个部分。

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

我想知道如何更改 ma​​in widget 中的 R、G、B 值,以使 opengl widget 中的背景颜色变为蓝色。

【问题讨论】:

  • 能否请您展示您创建OpenGLWidget 对象并使用它的代码段?你在MainWindow里面用过吗?
  • @qurban 我已在主小部件类 MainWindow 的问题中添加了请求的代码。

标签: python opengl pyqt pyopengl


【解决方案1】:

OpenGLWidget类中添加如下方法:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2021-12-11
    • 2015-06-26
    • 1970-01-01
    • 2011-09-30
    • 2017-06-14
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多