PyQt技术在2个源代码示例中存在差异

时间:2013-08-01 20:36:43

标签: python pyqt pyqt4

这两个程序的工作方式相同,但标有#HERE的行有一点不同。 有人可以解释这些差异吗?我不完全明白这些行是做什么的。

计划1:

import sys
from PyQt4 import QtGui, QtCore

class myform(QtGui.QDialog):
    def __init__(self, parent=None):
        super(myform, self).__init__(parent)

        form = QtGui.QFormLayout()
        form.setHorizontalSpacing(0)

        myedit = QtGui.QLineEdit()
        form.addWidget(myedit)

        self.setLayout(form)
        self.setGeometry(300, 300, 400, 0)
        self.setWindowTitle('test')

        myedit.textChanged.connect(self.editchange)   # new style signal slot connections

        self.show()                      # HERE

    def editchange(self,data):
        print "editchange:", data

if __name__ == "__main__":

    app = QtGui.QApplication([])
    ex = myform()

    #ex.exec_()                          # HERE
    #sys.exit(app.closeAllWindows())     # HERE

    sys.exit(app.exec_())                # HERE

计划#2:

import sys
from PyQt4 import QtGui, QtCore

class myform(QtGui.QDialog):
    def __init__(self, parent=None):
        super(myform, self).__init__(parent)

        form = QtGui.QFormLayout()
        form.setHorizontalSpacing(0)

        myedit = QtGui.QLineEdit()
        form.addWidget(myedit)

        self.setLayout(form)
        self.setGeometry(300, 300, 400, 0)
        self.setWindowTitle('test')

        myedit.textChanged.connect(self.editchange)   # new style signal slot connections

        #self.show()                      # HERE

    def editchange(self,data):
        print "editchange:", data

if __name__ == "__main__":

    app = QtGui.QApplication([])
    ex = myform()

    ex.exec_()                          # HERE
    sys.exit(app.closeAllWindows())     # HERE

    #sys.exit(app.exec_())                # HERE

1 个答案:

答案 0 :(得分:0)

程序#1从QApplication(http://pyqt.sourceforge.net/Docs/PyQt4/qapplication.html#exec)调用exec_

程序#2从QDialog(http://pyqt.sourceforge.net/Docs/PyQt4/qdialog.html#exec)调用exec_:结果对话框是模态对话框。

最终行为是相同的,因为您使用的是QDialog。

在这种情况下:

app = QtGui.QApplication([])
ex = myform()
ex.show()
app.exec_()

与:

相同
app = QtGui.QApplication([])
ex = myform()
ex.exec_()