问题描述:

I'm trying to create a GUI in with PyQt4, I initially used QT designer to get my overall layout done and then modify the code afterwards. What I have noticed is that there is a lot of repeating code that would be replaced with wither a class or method but I'm not sure which would be better or how exactly to go about implementing either way properly. here is an example of the repeating code (its a very large program)

self.ch2_R = QtGui.QLineEdit(Form)

self.ch2_R.setSizePolicy(sizePolicy)

self.ch2_R.setText(_fromUtf8(""))

self.ch2_R.setFrame(True)

self.ch2_R.setAlignment(QtCore.Qt.AlignCenter)

self.ch2_R.setReadOnly(True)

self.ch2_R.setObjectName(_fromUtf8("ch2_R"))

print self.ch2_R

self.ch3_R = QtGui.QLineEdit(Form)

self.ch3_R.setSizePolicy(sizePolicy)

self.ch3_R.setText(_fromUtf8(""))

self.ch3_R.setFrame(True)

self.ch3_R.setAlignment(QtCore.Qt.AlignCenter)

self.ch3_R.setReadOnly(True)

self.ch3_R.setObjectName(_fromUtf8("ch3_R"))

self.ch4_R = QtGui.QLineEdit(Form)

self.ch4_R.setSizePolicy(sizePolicy)

self.ch4_R.setText(_fromUtf8(""))

self.ch4_R.setFrame(True)

self.ch4_R.setAlignment(QtCore.Qt.AlignCenter)

self.ch4_R.setReadOnly(True)

self.ch4_R.setObjectName(_fromUtf8("ch4_R"))

sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)

sizePolicy.setHorizontalStretch(0)

sizePolicy.setVerticalStretch(0)

sizePolicy.setHeightForWidth(self.ch2_R.sizePolicy().hasHeightForWidth())

sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)

sizePolicy.setHorizontalStretch(0)

sizePolicy.setVerticalStretch(0)

sizePolicy.setHeightForWidth(self.ch3_R.sizePolicy().hasHeightForWidth())

sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)

sizePolicy.setHorizontalStretch(0)

sizePolicy.setVerticalStretch(0)

sizePolicy.setHeightForWidth(self.ch4_R.sizePolicy().hasHeightForWidth())

self.gridLayout.addWidget(self.ch2_R, 4, 5, 1, 4)

self.gridLayout.addWidget(self.ch3_R, 5, 5, 1, 4)

self.gridLayout.addWidget(self.ch4_R, 6, 5, 1, 4)

this code is all within

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):

My intial attempt was to creaet a method inside the Ui_Form class like this

def myQtlineEdit(self, name, form, sizePolicy):

self.name = QtGui.QLineEdit(Form)

self.name.setSizePolicy(sizePolicy)

self.name.setText(_fromUtf8(""))

self.name.setFrame(True)

self.name.setAlignment(QtCore.Qt.AlignCenter)

self.name.setReadOnly(True)

self.name.setObjectName(_fromUtf8("%s" %name))

sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)

sizePolicy.setHorizontalStretch(0)

sizePolicy.setVerticalStretch(0)

sizePolicy.setHeightForWidth(self.name.sizePolicy().hasHeightForWidth())

self myQtlineEdit("ch2_R", form, sizePolicy)

It did create the text field I was looking for but the

self.gridLayout.addWidget(self.ch2_R, 4, 5, 1, 4)

would give me an error saying that ch2_R did no exsist

Thanks.

网友答案:

Instead of creating a attribute named ch2_R you are always assigning to attribute self.name, you should instead do this

def myQtlineEdit(self, name, form, sizePolicy):
    widget = QtGui.QLineEdit(Form)
    widget.setSizePolicy(sizePolicy)
    widget.setText(_fromUtf8(""))
    widget.setFrame(True)
    widget.setAlignment(QtCore.Qt.AlignCenter)
    widget.setReadOnly(True)
    widget.setObjectName(_fromUtf8("%s" %name))

    setattr(self, name, widget)

    sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(widget.sizePolicy().hasHeightForWidth())
相关阅读:
Top