我是 Python 和 PyQT4 的新手。
我有一个简单的 QDialog Box,里面有一个 QlineEdit。
class Ui_UserCode(object):
def setupUi(self, UserCode):
UserCode.setObjectName(_fromUtf8("UserCode"))
UserCode.resize(265, 125)
UserCode.setMouseTracking(False)
self.lineEdit = QtGui.QLineEdit(UserCode)
self.lineEdit.setGeometry(QtCore.QRect(60, 50, 151, 31))
self.lineEdit.setFrame(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("returnPressed()")), UserCode.insertUC)
self.label = QtGui.QLabel(UserCode)
self.label.setGeometry(QtCore.QRect(60, 20, 151, 20))
self.label.setObjectName(_fromUtf8("label"))
self.buttonBox = QtGui.QDialogButtonBox(UserCode)
self.buttonBox.setGeometry(QtCore.QRect(40, 90, 180, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.retranslateUi(UserCode)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), UserCode.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), UserCode.reject)
QtCore.QMetaObject.connectSlotsByName(UserCode)
def retranslateUi(self, UserCode):
UserCode.setWindowTitle(_translate("UserCode", "Dialog", None))
self.label.setText(_translate("UserCode", "Please Enter User Code", None))
self.lineEdit.setText(_translate("Dialog", "", None))
这行代码在我创建的 QtableWidget 的单独类中。
item.setText(_translate("MainWindow", "" , None))
然后...我的初始化类和 pyqtSlot()
class StartUC(QDialog):
def __init__(self):
QDialog.__init__(self)
self.uc = Ui_UserCode()
self.uc.setupUi(self)
self.ui = Ui_MainWindow()
@QtCore.pyqtSlot()
def insertUC(self):
inputNumber = self.uc.lineEdit.text()
tableWidget = StartMain(QTableWidgetItem)
item = tableWidget.item(0, 1)
tableWidget.item.setText("MainWindow", "%s"% inputNumber, None)
class StartMain(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
我的问题是如何让对话框的 QlineEdit 在 returnPressed 事件上填充 QtableWidgetCell?我的 pyqtSlot() 方法是否在正确的位置?我意识到我还有很多东西要学,所以如果有人能指出正确的方向,那就太棒了。PyQT 文档给了我一个不错的开始,但我不知道如何完成这个......