Getting my feet wet...
-
Hi I'm new to the PyQt framework, and Python in general and I need some help with a few functions that are driving me crazy. I've tried reading through the documentation as best I could but to no avail. Here is my problem....
I have this simple input box that I would like a user to sumbit code into....
@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.returnPressed) 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))@
-
Seems this is just a pyuic-generated file. You must write your own python code to use this class.
-
And... This simple table widget window that I would like the UserCode text() to be enter into... I'm only posting the retranslationUi method because the rest is too much code...
@def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.tableWidget.setSortingEnabled(True)
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "DATE/TIME", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "USER", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "RECIPE#", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "MATERIAL", None))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "LOT#", None))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "BATCH#", None))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "TARE WT.", None))
item = self.tableWidget.horizontalHeaderItem(7)
item.setText(_translate("MainWindow", "PRODUCT WT.", None))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 1)
item.setText(_translate("MainWindow", "" , None))
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.label.setText(_translate("MainWindow", "Database version: %s " % vdata, None))
self.pushButton.setText(_translate("MainWindow", "Add Weight", None))@I realize that the @item.setText(_translate("MainWindow", "" , None))@ is the place to insert a variable with a % but how do I go about referencing it from a totally different Class. I've also tried making If statements to insert the variables on pressedReturn() signal but that didn't seem to work either and the signal also gives and error... I'm stumped, any input would be greatly appreciated. Oh and I'm using Python 2.7 and PyQt4.
-
Hi, modify the pyuic-generated file is not a good idea.
-
I have finally gotten around to re-writing and getting my main class and code written to post and I still seem to be having a problem.
I have this QDialog Window box in a pyuic-generated file that pops up with a QlineEdit text box that a user can submit a code to and press enter. It then executes the insertUC SIGNAL to dump the data from the QlineEdit to somewhere else...
@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))@
This is the init Class I have in a separate file called init which is started by a standard if statement.
@class StartUC(QDialog):
def init(self):
QDialog.init(self)
self.uc = Ui_UserCode()
self.uc.setupUi(self)@QtCore.pyqtSlot() def insertUC(self): inputNumber = self.uc.lineEdit.text() mainInput = QtGui.QTableWidget() item = QTableWidgetItem(0,1, "ITEM 1") if inputNumber == 1: info = inputNumber mainInput.item.setText("%s" % info)@
I do realize that the above code gives and an error on the QTableWidgetItem(0,1, "ITEM 1") There has been a lot of trial and error in this function and I have yet to correct it...
As you can see above I have the lineEdit.text in the called method defined but it doesn't seem to want to output to a specific cell in my QTableWidget Mainwindow object that I have also made in the same interface file as stated above. As it is a little too much code to post here is the retranslate method from it:@def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.tableWidget.setSortingEnabled(True)
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "DATE/TIME", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "USER", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "RECIPE#", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "MATERIAL", None))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "LOT#", None))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "BATCH#", None))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "TARE WT.", None))
item = self.tableWidget.horizontalHeaderItem(7)
item.setText(_translate("MainWindow", "PRODUCT WT.", None))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
#item = self.tableWidget.item(0, 1)
#item.setText(_translate("MainWindow", "" , None))
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.label.setText(_translate("MainWindow", "Database version: %s " % vdata, None))
self.pushButton.setText(_translate("MainWindow", "Add Weight", None))
@The 2 lines that I have commented out are where I need my QlineEdit function to send the user inputted text... but it seems do to my lack of knowledge in Python, something stupid simple is escaping me. I have tried several different things but am considering now as I stare blankly into the abyss that is my computer monitor that I may just have no idea what I am doing...
Here are a few things I've considered...
- The if statement called by InputUC function are in the StartUC class which is a completely separate class than the MainWindow and I may just need to add them together and start them at the same time or learn how to define a class within a class (aka static methods)...?
- I need different Qt functions like QTableWidgetItem.editItem() or .setCurrentCell() in place in order for the insertUC function to know where to put the data prior to me using the if statement.