0

这 2 个程序的工作方式相同,但标有 #HERE 的行略有不同。有人可以解释这些差异吗?我不完全理解这些行的作用。

方案一:

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
4

1 回答 1

0

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

exec_来自 QDialog ( http://pyqt.sourceforge.net/Docs/PyQt4/qdialog.html#exec ) 的程序 #2 调用:生成的对话框是模态对话框。

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

在这种情况下:

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

是相同的:

app = QtGui.QApplication([])
ex = myform()
ex.exec_()
于 2013-08-02T00:28:18.867 回答