2012-01-28 105 views
4

我試圖通過單擊主窗口中的按鈕來啓動對話框:這裏是(qtdesigner生成)代碼,我修改它只是爲了測試它..我設置了showDial功能可以在單擊按鈕時顯示撥號。但它不工作:QDialog不能從主窗口打開(pyQt)

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    _fromUtf8 = lambda s: s 



class Ui_Dialog(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName(_fromUtf8("Dialog")) 
     Dialog.setWindowModality(QtCore.Qt.WindowModal) 
     Dialog.resize(400, 300) 
     Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8)) 

     self.retranslateUi(Dialog) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 

    def retranslateUi(self, Dialog): 
     pass 

class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.resize(309, 148) 
     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setGeometry(QtCore.QRect(50, 30, 191, 71)) 
     self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Dialog", None, QtGui.QApplication.UnicodeUTF8)) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     MainWindow.setCentralWidget(self.centralwidget) 

     self.retranslateUi(MainWindow) 

     QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked(QAbstractButton*)")), self.showDial) 

     QtCore.QMetaObject.connectSlotsByName(MainWindow) 



    def retranslateUi(self, MainWindow): 
     pass 

    def showDial(self): 
     Dialog = QtGui.QDialog() 
     u = Ui_Dialog() 
     u.setupUi(Dialog) 

     Dialog.exec_() 


if __name__ == "__main__": 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    MainWindow = QtGui.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    sys.exit(app.exec_()) 

回答

3

有信號連接錯誤,應該是:

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.showDial) 

或更Python New-style Signal and Slot syntax爲PyQt的4.5+:

+0

感謝。這解決了它 – flak37 2012-01-28 09:11:08