I am trying to learn pyQt programming in a MVC pattern(which in QT there is not controller, so really just Model/View). I have try1.py which is the graphics I made from QT designer. In that, I have 2 line edit boxes- Username and Password. Then I have test.py which is the actual python script.
1) Since the text lines widgets are nested in the global Ui_Form....how would I assign the model to them? typically I would use something like widgetView = setModel(model)
but I don't know how to do this with self.lineEdit = QtGui.QLineEdit(Form)
. Since I am trying to use MVC I assume this needs to be outside of Ui_Form class?
2) Would I have 2 different models- 1 for username widget and 1 for password?
3) How would I get the TextLineModel to update the fields in the X object instance and the View get updated as well?
This is the try1.py QT Editor already compiled by pyuic.py:
# -*- 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))
This is the script test.py script:
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_())