0

我正在尝试以 MVC 模式学习 pyQt 编程(在 QT 中没有控制器,所以实际上只是模型/视图)。我有 try1.py,这是我从 QT 设计器制作的图形。在那,我有 2 行编辑框 - 用户名和密码。然后我有 test.py 这是实际的 python 脚本。

1)由于文本行小部件嵌套在全局 Ui_Form 中......我如何将模型分配给它们?通常我会使用类似的东西,widgetView = setModel(model)但我不知道如何使用 self.lineEdit = QtGui.QLineEdit(Form). 由于我正在尝试使用 MVC,我认为这需要在 Ui_Form 类之外?

2)我会有 2 种不同的模型 - 1 用于用户名小部件,1 用于密码?

3) 我如何让 TextLineModel 更新 X 对象实例中的字段并且视图也得到更新?

这是pyuic.py已经编译好的try1.py QT Editor:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'try1.ui'
#
# Created: Mon May 27 04:00:05 2013
#      by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

    class Ui_Form(object):
        def setupUi(self, Form):
            Form.setObjectName(_fromUtf8("Form"))
            Form.resize(640, 480)
            self.label = QtGui.QLabel(Form)
            self.label.setGeometry(QtCore.QRect(100, 30, 71, 16))
            self.label.setObjectName(_fromUtf8("label"))
            self.label_2 = QtGui.QLabel(Form)
            self.label_2.setGeometry(QtCore.QRect(100, 60, 61, 16))
            self.label_2.setObjectName(_fromUtf8("label_2"))
            self.lineEdit = QtGui.QLineEdit(Form)
            self.lineEdit.setGeometry(QtCore.QRect(160, 30, 231, 20))
            self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
            self.lineEdit_2 = QtGui.QLineEdit(Form)
            self.lineEdit_2.setGeometry(QtCore.QRect(160, 60, 231, 20))
            self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
            self.verticalLayoutWidget = QtGui.QWidget(Form)
            self.verticalLayoutWidget.setGeometry(QtCore.QRect(80, 20, 361, 81))
            self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
            self.verticalLayout_2 = QtGui.QVBoxLayout(self.verticalLayoutWidget)
            self.verticalLayout_2.setMargin(0)
            self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))

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

        def retranslateUi(self, Form):
            Form.setWindowTitle(_translate("Form", "Form", None))
            self.label.setText(_translate("Form", "UserName:", None))
            self.label_2.setText(_translate("Form", "Password:", None))

这是脚本 test.py 脚本:

import sys
from PyQt4 import QtCore, QtGui

from untitled import Ui_Form
from collections import OrderedDict

class GetInfo():
    def __init__(self):
        self.info = OrderedDict([('userName', None),
                     ('passWord', ' ')]

    def login_name(self):
        name = raw_input("Enter Login Name: ")
        self.login_info["login_name"] = name

    def password(self):
        name = raw_input("Enter Password: ")
        self.login_info["password"] = name


class TextLineModel(QtCore.QAbstractListModel):
        def __init__(self, text, parent = none):
             QtCore.QAbstractListModel.__init__(self, parent)
             self._text = text

         def data(self, index, roll):
                if role == QtCore.Qt.DisplayRole:
                    value = self._text
                    return value



class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)


if __name__ == "__main__":
    x = GetInfo()
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())
4

1 回答 1

2

1)您必须使用QDataWidgetmapper链接您的模型和小部件。

2)不,你应该有一个单一的数据类别的模型。在您的情况下,它应该是QAbstractTablemodel.

3)您可以调用QDataWidgetmapper.revert()以使用模型中的数据更新小部件。模型要么在用户完成小部件编辑后自动更新,要么通过手动调用QDataWidgetmapper.submit()(参见 参考资料QDataWidgetmapper.submitPolicy

于 2013-05-27T11:43:11.787 回答