2014-09-03 38 views
-2

我在Python和PySide中編寫了一個簡單的應用程序。當我運行它時,SIGNAL不起作用。 應用程序無錯啓動。信號在PySide中不起作用

from PySide.QtCore import * 
from PySide.QtGui import * 
import sys 

class Form(QDialog): 

    def __init__(self, parent=None): 
     super(Form, self).__init__(parent) 

     dial = QDial() 
     dial.setNotchesVisible(True) 

     spinbox = QSpinBox() 

     layout = QHBoxLayout() 
     layout.addWidget(dial) 
     layout.addWidget(spinbox) 
     self.setLayout(layout) 

     self.connect(dial, SIGNAL("valueChaged(int)"), spinbox.setValue) 
     self.connect(spinbox, SIGNAL("valueChaged(int)"), dial.setValue) 

     self.setWindowTitle("Signals and Slots") 
    # END def __init__ 
# END class Form 

def main(): 
    app = QApplication(sys.argv) 
    form = Form() 
    form.show() 
    app.exec_() 
# END def main 

if __name__ == '__main__': 
    main() 
# END if 

我使用:

Pyside 1.2.2; Python 2.7.6; OS Centos; Windows 7的

我運行應用程序時出現

崇高的文本3和Eclipse月神;

我該如何讓SIGNALs工作?

+0

Btw。使用新的樣式信號語法'dial.valueChanged.connect(spinbox.setValue)',您將會遇到錯誤。 – Trilarion 2014-09-04 08:42:38

回答

1

您的信號名稱不正確;

錯誤:

valueChaged (int) 

Correct

valueChanged (int) 

測試它,做工精細;

import sys 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class QFormDialog (QDialog): 
    def __init__(self, parent = None): 
     super(QFormDialog, self).__init__(parent) 
     self.myQial = QDial() 
     self.myQSpinbox = QSpinBox() 
     self.myQHBoxLayout = QHBoxLayout() 
     self.myQial.setNotchesVisible(True) 
     self.myQHBoxLayout.addWidget(self.myQial) 
     self.myQHBoxLayout.addWidget(self.myQSpinbox) 
     self.setLayout(self.myQHBoxLayout) 
     self.connect(self.myQial,  SIGNAL('valueChanged(int)'), self.myQSpinbox.setValue) 
     self.connect(self.myQSpinbox, SIGNAL('valueChanged(int)'), self.myQial.setValue) 
     self.setWindowTitle('Signals and Slots') 

if __name__ == '__main__': 
    myQApplication = QApplication(sys.argv) 
    myQFormDialog = QFormDialog() 
    myQFormDialog.show() 
    myQApplication.exec_() 

注:PyQt4的& PySide是同樣的方式來實現。

+1

謝謝你的回答。我的錯。 – Romulus 2014-09-04 11:58:22