PyQt5 QDateEdit()撤消(PyQt5 QDateEdit() undo)

编程入门 行业动态 更新时间:2024-10-28 13:16:17
PyQt5 QDateEdit()撤消(PyQt5 QDateEdit() undo)

在酒店预订系统中,我有几个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.

更多推荐

本文发布于:2023-07-24 20:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QDateEdit   undo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!