来自PyQt5形式的OpenGL小部件交互作为单独的线程

时间:2018-12-13 09:31:06

标签: python python-3.x opengl pyqt5

我需要以PyQt5形式在OpenGL小部件上添加图形(几何图形)。

这里有一个示例,其中我有3个线程和一个主线程作为表单运行器 2个线程仅在按下按钮后将数字传递到文本框 而且我需要以相同的方式添加一个线程,在按下按钮self.pushButton_GL之后,该函数将以相同的形式在OpenGL小部件上绘制一个轴,并且在其他2个线程仍在运行且主要表格不冻结。

所以,这里有一些代码,以防将来有人和我一样需要这个东西。

我的进口商品:

import sys
import time
from PyQt5 import QtCore
import threading
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication, QDialog, QOpenGLWidget
from PyQt5.QtCore import pyqtSignal
import pyqtgraph.opengl as gl

主窗体初始化类:

class form_thread(QDialog):
    def __init__(self):
        super(form_thread,self).__init__()
        loadUi('thread_test.ui', self)
        self.setGeometry(800, 300, 537, 831)
        self.setWindowTitle("window")

        self.pushButton.clicked.connect(lambda: self.startTheThread())
        self.pushButton1.clicked.connect(lambda:self.startTheThread_thread2())
        self.pushButton_GL.clicked.connect(lambda:self.startTheThread_thread3())

线程#1和#2:

# This is Thread #1
    def theCallbackFunc(self, msg):
        print('thread #1 has sent this message to the GUI:')
        print(msg)
        self.textEdit.append("Counter: " + msg)

    def startTheThread(self):
        id_0 = (self.lineEdit.text())
        t = threading.Thread(name='myThread', target=myThread, args=(self.theCallbackFunc, int(id_0)))
        t.start()

# This is Thread #2
    def theCallbackFunc_thread2(self, msg):
        print('thread #2 has sent this message to the GUI:')
        print(msg)
        self.textEdit1.append("Counter: " + msg)

    def startTheThread_thread2(self):
        id_1 = (self.lineEdit1.text())
        t2 = threading.Thread(name='myThread2', target=myThread_2, args=(self.theCallbackFunc_thread2, int(id_1)))
        t2.start()

这里应该是线程#3,我正在其中连接到OpenGL小部件

# This is Thread #3 OpenGL Thread
    def theCallbackFunc_thread3(self, msg):
        print('thread #3 has sent this message to the GUI:')
        print(msg)
        self.textEdit1.append("Counter: " + msg)

    def startTheThread_thread3(self):
        id_2 = (self.lineEdit2.text())
        t3 = threading.Thread(name='myThread3', target=myThread_3, args=(self.theCallbackFunc_thread3, int(id_2)))
        t3.start()

然后我需要将类传递给主线程:

class Communicate(QtCore.QObject):
    myGUI_signal = QtCore.pyqtSignal(str)
    myGUI_signal2 = QtCore.pyqtSignal(str)
    myGUI_signal3 = QtCore.pyqtSignal(str)


def myThread(callbackFunc, id):
    mySrc = Communicate()
    mySrc.myGUI_signal.connect(callbackFunc)
    for x in range (1, id):
        msgForGui = "1, %s" %x
        mySrc.myGUI_signal.emit(msgForGui)
        time.sleep(0.8)


def myThread_2(callbackFunc, id):
    mySrc = Communicate()
    mySrc.myGUI_signal2.connect(callbackFunc)
    for x in range (1, id):
        msgForGui = "1, %s" %x
        mySrc.myGUI_signal2.emit(msgForGui)
        time.sleep(0.5)

最后,这里应该是一个函数,实际上是我在制作一个几何并将其传递给OpenGL小部件:

因此,实际上,我有一个最大的问题,如何正确实现对主窗体的回调,以便在OpenGL小部件中出现某些东西?,因为现在我不知道如何实现这个。

def myThread_3(callbackFunc, id):
    mySrc = Communicate()
    mySrc.myGUI_signal3.connect(callbackFunc)

    # show axis at OpenGL widget [name(openGLWidget)] at PyQT5 Form
    w = gl.GLViewWidget()
    # Show axis at OpenGL widget
    axis = gl.GLAxisItem()
    axis.translate(0, 0, 0)
    w.addItem(axis)

和最终通话:

app = QApplication(sys.argv)
form = form_thread()
form.show()
sys.exit(app.exec_())

表单快照: thread_test.ui

0 个答案:

没有答案