pyqt qdialog 默认按钮,QDialog不能从主窗口(pyQt)打开

I'm trying to launch a dialog by clicking a button in the main window: Here's the (qtdesigner generated) code which I modified just to test it .. I've set the showDial function to show the dial when the button is clicked. But it doesn't work :

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_())

解决方案

There is error in signal connection, it should be:

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

or in more pythonic New-style Signal and Slot syntax for PyQt 4.5+:

self.pushButton.clicked.connect(self.showDial)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值