我已经在我的pyside程序中编写了一个命令行小部件。结构是这样的:
class CommandWidget(QWidget):
def __init__(self, parent = None):
super(CommandWidget, self).__init__(parent)
self.buffer=PyInterp(self)
self.buffer.initInterpreter(locals())
self........
class PyInterp(QTextEdit):
class InteractiveInterpreter(code.InteractiveInterpreter):
def __init__(self, locals):
code.InteractiveInterpreter.__init__(self, locals)
def runIt(self, command):
code.InteractiveInterpreter.runsource(self, command)
def __init__(self, parent = None):
super(PyInterp, self).__init__(parent)
我还有一个与其他一些小部件一起运行的主窗口程序。我的问题是我如何在此解释器中导入其他小部件类中的某些函数,运行该函数并将结果输出到全局空间。或者换句话说,我想在解释器的局部空间和全局主窗口空间之间共享一些变量。我怎样才能做到这一点?
编辑:这是我想放入信号中的数据类型。
class PosType(QObject):
def __init__(self, nx, ny, nz, start_pos, type):
self.nx = nx
self.ny = ny
self.nz = nz
self.start_pos = start_pos
self.type = type
这是信号。
class PosSig(QObject):
sig = Signal(PosType)
def emit_sig(self, pos_data):
self.sig.emit(pos_data)
这就是我想放入解释器中的函数,以便在调用时它会发出信号。
def graphene(nx, ny, start_pos):
pos_info = PosType(nx = nx, ny = ny, nz = None, start_pos = start_pos, type = 1)
tmp_sig = PosSig()
tmp_sig.emit_sig(pos_info)
return
上面的类都在一个名为 ExposeFunc.py 的文件中,我打算在解释器中导入这个.py文件,然后调用石墨烯函数发出信号。
在主窗口类中,我有一个插槽。
def __init__(self):
#Interpreter Signals :
possig = PosSig()
possig.sig.connect(self.createObject)
@Slot(PosType)
def createObject(self, pos_info):
type = pos_info.type
if type == 1:
SharedItems.QS._FillData(pos_info.nx, pos_info.ny, start_pos)
return
有几种机制。您可以使用QtCore.Signal()
和@QtCore.Slot()
,如果您有在某个点传递的内容。您可以将大多数内容放入Signal
请参阅信号/插槽示例。Qt Signal & Slots 文档
我不太精通的另一种机制是QQueue
类。需要解决的问题是跨线程传递数据,因此需要保护数据访问。
信号/插槽示例:
class myDataType(QObject):
def __init__(self, data):
self.data = data
...
class foo(QObject):
mySignal = QtCore.Signal(myDataType)
def __init__(self):
...
def someFunction(self, data):
mySignal.emit(data)
class bar(QObject):
def __init__(self):
self.otherObject = foo()
self.otherObject.mySignal.connect(self.handler)
@QtCore.Slot(myDataType)
def handler(self, data):
do something with data
增编1:
假设您有QMainWindow
class myMainWindow(QtGui.QMainWindow):
mainWindowSignal = QtCore.Signal(QObject)
def __init__(self, parent, *vargs, **kwargs):
...
self.myCommandWidget = CommandWidget(parent=self)
self.myCommandButton = QtGui.QPushButton("Press Me")
#This connects the button being clicked to a function.
self.myCommandButton.clicked.connect(self.button_pressed)
#This connects the Signal we made 'mainWindowSignal' to
# the do_something Slot in CommandWidget 'myCommandWidget'
self.mainWindowSignal.connect(self.myCommandWidget.do_something)
#This connects the Signal from 'myCommandWidget' 'dataReady'
# to Slot 'data_returned' to handle the data
self.myCommandWidget.dataReady.connect(self.dataReady)
self.data = "some data"
#We don't have to decorate this, but should
@QtCore.Slot()
def button_pressed(self):
self.mainWindowsSignal.emit(self.data)
@QtCore.Slot(str, int)
def data_returned(self, strValue, intValue):
#do something with the data.
#e.g.
self.command = strValue
self.retCode = intValue
class CommandWidget(QWidget):
dataReady = QtCore.Signal(str, int)
def __init__(self, parent=None):
#stuff you had here.
...
@QtCore.Slot(QObject
def do_something(self, data):
retStr = self.buffer.... #insert your function calls here
retInt = self.buffer....
self.dataReady.emit(retValue, retInt)