在酒店预订系统中,我有几个QDateEdit字段映射到我的表单中的数据库记录。 用户更新任何这些字段后,程序会进行检查以确保房间可用。 如果房间不可用,我想将QDateEdit恢复到原始值,并让用户知道QMessageBox。
QLineEdit具有一种按照我的意图工作的撤消方法。
此外,如果我试图通过从模型中提取信息来重置QDateEdit,它会再次触发dateChanged信号。 这不是一个大问题,因为我知道原始值是有效的,但对我来说似乎是浪费。
如何在不生成dateChanged信号的情况下撤销对QDateEdit的更改?
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Guest_form(QDialog): def __init__(self, parent=None): QDialog.__init__(self) self.date_good = True self.date_edit = QDateEdit() self.line_edit = QLineEdit() layout = QVBoxLayout() layout.addWidget(self.date_edit) layout.addWidget(self.line_edit) self.setLayout(layout) self.date_edit.setFocus() self.date_edit.dateChanged.connect(self.check_date) self.line_edit.editingFinished.connect(self.check_date) def check_date(self): self.line_edit.setText(self.date_edit.date().toString()) self.date_good = False #self.date_edit.undo() #How? self.date_edit.setDate(QDate.currentDate()) self.line_edit.undo() print('here') if __name__=="__main__": app=QApplication(sys.argv) myapp = Guest_form() myapp.show() sys.exit(app.exec_())In a hotel booking system, I have a couple of QDateEdit fields mapped to a database record in my form. After the user updates any of these fields, the program makes a check to ensure that the room is available. If the room is not available, I would like to restore the QDateEdit to its original value and let the user know with a QMessageBox.
QLineEdit has an undo method which works as I intend.
Furthermore, if I try to reset the QDateEdit by pulling the information from the model, it triggers the dateChanged signal again. This isn't a huge problem as I know the original value is valid but seems wasteful to me.
How can I undo a change to a QDateEdit without generating the dateChanged signal?
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Guest_form(QDialog): def __init__(self, parent=None): QDialog.__init__(self) self.date_good = True self.date_edit = QDateEdit() self.line_edit = QLineEdit() layout = QVBoxLayout() layout.addWidget(self.date_edit) layout.addWidget(self.line_edit) self.setLayout(layout) self.date_edit.setFocus() self.date_edit.dateChanged.connect(self.check_date) self.line_edit.editingFinished.connect(self.check_date) def check_date(self): self.line_edit.setText(self.date_edit.date().toString()) self.date_good = False #self.date_edit.undo() #How? self.date_edit.setDate(QDate.currentDate()) self.line_edit.undo() print('here') if __name__=="__main__": app=QApplication(sys.argv) myapp = Guest_form() myapp.show() sys.exit(app.exec_())最满意答案
你可以使用Qt的Undo / Redo Framework。 你可以在这里找到一个很好的介绍。
You can use Qt's Undo/Redo Framework. You can find a good introduction here.
更多推荐
发布评论