我需要修改多个QLineEdit,例如设置默认值或检查输入是否为整数。我决定尝试使用类,以便组织代码。
from PyQt5 import QtWidgets, QtCore, QtGui
from Ui_Dla_Szefa import UiDlaSzefa
class LineEdit(QtWidgets.QLineEdit):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def set_text(self):
self.setText("0")
class InputWindow(QtWidgets.QMainWindow, UiDlaSzefa):
def __init__(self):
super().__init__()
self.setupUi(self)
self.show()
self.lineEdit_otrzymane1 = LineEdit()
self.lineEdit_otrzymane1.set_text()
很遗憾,没有任何反应。问题似乎在于将输入从QLineEdit传输到lineEdit_otrzymane1。请帮助 :) 也许有一种更好的方法可以在一处更改多个lineEdit?
答案 0 :(得分:0)
如果您应用程序的QLineEdit::objectName
具有语义分组,这似乎很有意义。
下面的示例演示如果对象的名称包含字符串QLineEdit
,则替换'otrzymane'
小部件的值:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class UiDlaSzefa(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1029, 856)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout_12 = QtWidgets.QGridLayout(self.centralWidget)
self.gridLayout_12.setContentsMargins(11, 11, 11, 11)
self.gridLayout_12.setSpacing(6)
self.gridLayout_12.setObjectName("gridLayout_12")
self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.widget = QtWidgets.QWidget(self.tab)
self.widget.setGeometry(QtCore.QRect(0, 0, 241, 131))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(11, 11, 11, 11)
self.gridLayout.setHorizontalSpacing(4)
self.gridLayout.setVerticalSpacing(0)
self.gridLayout.setObjectName("gridLayout")
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 4, 0, 1, 1)
self.label_11 = QtWidgets.QLabel(self.widget)
self.label_11.setObjectName("label_11")
self.gridLayout.addWidget(self.label_11, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.widget)
self.label.setMaximumSize(QtCore.QSize(16777215, 20))
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 2)
self.label_14 = QtWidgets.QLabel(self.widget)
self.label_14.setObjectName("label_14")
self.gridLayout.addWidget(self.label_14, 2, 2, 1, 1)
self.label_24 = QtWidgets.QLabel(self.widget)
self.label_24.setObjectName("label_24")
self.gridLayout.addWidget(self.label_24, 1, 2, 1, 1)
self.label_4 = QtWidgets.QLabel(self.widget)
self.label_4.setObjectName("label_4")
self.gridLayout.addWidget(self.label_4, 4, 2, 1, 1)
self.label_10 = QtWidgets.QLabel(self.widget)
self.label_10.setObjectName("label_10")
self.gridLayout.addWidget(self.label_10, 3, 0, 1, 1)
self.label_13 = QtWidgets.QLabel(self.widget)
self.label_13.setObjectName("label_13")
self.gridLayout.addWidget(self.label_13, 3, 2, 1, 1)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 2, 1, 2)
self.label_12 = QtWidgets.QLabel(self.widget)
self.label_12.setObjectName("label_12")
self.gridLayout.addWidget(self.label_12, 1, 0, 1, 1)
self.lineEdit_otrzymane1 = QtWidgets.QLineEdit(self.widget)
# ...
# ...
self.lineEdit_w1_2.setText(_translate("MainWindow", "0"))
self.lineEdit_i1_2.setText(_translate("MainWindow", "0"))
self.lineEdit_i5_2.setText(_translate("MainWindow", "0"))
self.lineEdit_w200_2.setText(_translate("MainWindow", "0"))
self.lineEdit_i10_2.setText(_translate("MainWindow", "0"))
self.label_140.setText(_translate("MainWindow", "0,20zł X"))
self.lineEdit_w5_2.setText(_translate("MainWindow", "0"))
self.label_146.setText(_translate("MainWindow", "0,05zł X"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
# ---------------------------------------------------------------------------
class InputWindow(QtWidgets.QMainWindow, UiDlaSzefa):
def __init__(self):
super().__init__()
self.setupUi(self)
self.show()
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.lineEdit_X = QtWidgets.QLineEdit()
self.lineEdit_X.setWindowTitle("Set default value and to check whether the input is integer")
self.lineEdit_X.setGeometry(700, 100, 270, 50)
self.lineEdit_X.editingFinished.connect(self.on_editingFinished)
self.lineEdit_X.show()
@QtCore.pyqtSlot()
def on_editingFinished(self):
try:
initLineInt = int(self.lineEdit_X.text())
initLineStr = self.lineEdit_X.text()
for widget in QtWidgets.qApp.allWidgets():
if isinstance(widget, QtWidgets.QLineEdit):
if 'otrzymane' in widget.objectName(): # 'otrzymane' !!!
print(widget.objectName())
widget.setText(initLineStr)
except ValueError:
pass
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
show_all = InputWindow()
show_all.show()
sys.exit(app.exec_())
答案 1 :(得分:0)
仅当我想清除整个电子表格时,上一个答案才有效。 有多种解决方案,例如使用mask(要覆盖的选项太多),使用上一示例中的迭代方法,但最后我决定尽可能使用QDoubleSpinBox。它具有内置掩码,格式化输入没有问题(许多国家/地区使用“,”代替“。”。
如果不是遮罩问题,则子类化将是一个不错的解决方案:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
####################################################################
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
####################################################################
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
# create objects
self.la = QLabel("Press tab in this box:")
self.le = MyLineEdit()
self.la2 = QLabel("\nLook here:")
self.le2 = QLineEdit()
# layout
layout = QVBoxLayout()
layout.addWidget(self.la)
layout.addWidget(self.le)
layout.addWidget(self.la2)
layout.addWidget(self.le2)
self.setLayout(layout)
# connections
self.connect(self.le, SIGNAL("tabPressed"),
self.update)
def update(self):
newtext = str(self.le2.text()) + "tab pressed "
self.le2.setText(newtext)
####################################################################
class MyLineEdit(QLineEdit):
def __init__(self, *args):
QLineEdit.__init__(self, *args)
def event(self, event):
if (event.type()==QEvent.KeyPress) and (event.key()==Qt.Key_Tab):
self.emit(SIGNAL("tabPressed"))
return True
return QLineEdit.event(self, event)
####################################################################
if __name__ == "__main__":
main()
https://www.saltycrane.com/blog/2008/01/how-to-capture-tab-key-press-event-with/