I am currently working in a panel in Nuke 11 that opens a QDialog and I was wondering how to get a value from it into my main class when I close my QDialog ? The QDialog is in a different class. This is a simplified example that shows my problem :
import nuke
from nukescripts import panels
try:
## < Nuke11
import PySide.QtGui as QtGui
import PySide.QtCore as QtCore
except:
## >= Nuke11
import PySide2.QtCore as QtCore
import PySide2.QtGui as QtGui
import PySide2.QtWidgets as QtGui
from PySide2.QtWidgets import QWidget as QWidget
class Example(QtGui.QWidget):
def __init__(self):
super(Example,self).__init__()
layout = QtGui.QVBoxLayout()
button = QtGui.QPushButton('Get Value')
button.clicked.connect(self.someFunction)
layout.addWidget(button)
self.setLayout(layout)
def someFunction(self):
value = self.GetValueLineEdit()
if value :
# do something
def GetValueLineEdit(self):
class getValuePanel(QtGui.QDialog):
def __init__(self):
super(getValuePanel, self).__init__()
layout = QtGui.QHBoxLayout()
self.lineEdit = QtGui.QLineEdit('')
getValueButton = QtGui.QPushButton('Get Value')
getValueButton.clicked.connect(self.getValue)
layout.addWidget(self.lineEdit)
layout.addWidget(getValueButton)
self.setLayout(layout)
def getValue(self):
value = str(self.lineEdit.text())
getValuePanel.accept() #To Close the QDialog
return value
getValuePanel = getValuePanel()
getValuePanel.show()
pane = nuke.getPaneFor("Example.panel")
panels.registerWidgetAsPanel('Example', 'Example',"", True).addToPane(pane)
Thanks a lot,
First of all you do not create classes within other classes, it is considered a bad practice. On the other hand what returns a slot is not used since no one receives it, so what returns getValue will be lost, what you must do is that the clicked call accept closing the QDialog and after verifying that the dialogue has been accepted you just have to call getValue:
class ValuePanel(QtGui.QDialog):
def __init__(self):
super(ValuePanel, self).__init__()
self.lineEdit = QtGui.QLineEdit()
getValueButton = QtGui.QPushButton('Get Value')
getValueButton.clicked.connect(self.accept)
layout = QtGui.QHBoxLayout(self)
layout.addWidget(self.lineEdit)
layout.addWidget(getValueButton)
def getValue(self):
value = str(self.lineEdit.text())
return value
class Example(QtGui.QWidget):
def __init__(self):
super(Example,self).__init__()
button = QtGui.QPushButton('Get Value')
button.clicked.connect(self.someFunction)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(button)
@QtCore.Slot()
def someFunction(self):
value_panel = ValuePanel()
if value_panel.exec_() == QtGui.QDialog.Accepted:
print(value_panel.getValue())
来源:https://stackoverflow.com/questions/53178290/how-to-get-value-from-qdialog-using-multiple-class