MVCパターンでpyQtプログラミングを学習しようとしています(QTにはコントローラーがないため、実際にはモデル/ビューのみです)。QTデザイナーから作成したグラフィックであるtry1.pyがあります。その中に、ユーザー名とパスワードの 2 行の編集ボックスがあります。次に、実際の python スクリプトである test.py があります。
1) テキスト行ウィジェットはグローバル Ui_Form にネストされているため、モデルをどのように割り当てますか? 通常、次のようなものを使用widgetView = setModel(model)
しますが、 でこれを行う方法がわかりません self.lineEdit = QtGui.QLineEdit(Form)
。私は MVC を使用しようとしているので、これは Ui_Form クラスの外にある必要があると思いますか?
2) ユーザー名ウィジェット用とパスワード用の 2 つの異なるモデルがありますか?
3) TextLineModel で X オブジェクト インスタンスのフィールドを更新し、View も更新するにはどうすればよいですか?
これは、すでに pyuic.py によってコンパイルされている try1.py QT エディターです。
# -*- 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_())