How to get value from QDialog using multiple class

前端 未结 1 394
感情败类
感情败类 2021-01-24 16:04

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

相关标签:
1条回答
  • 2021-01-24 16:48

    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())
    
    0 讨论(0)
提交回复
热议问题