简体   繁体   中英

PyQT5: How to use both QLineEdit: Validator and InputMask?

I want to use both InputMask and Validator to get date in the correct form. In the code below I am using InputMask to receive date in format DD.MM.YYYY. I do not know how to limit each part of it (DD, MM and YYYY) because now user can type 40.30.2020 and it is theoretically correct.

self.date = QLineEdit(self)
self.date.setInputMask("00.00.0000")

QDateTimeEdit Class

The QDateTimeEdit class provides a widget for editing dates and times.

import sys
from PyQt5.QtCore import QDate 
from PyQt5.QtWidgets import (QApplication, QWidget, QDateTimeEdit, 
                             QFormLayout, QLabel)
from PyQt5.QtGui import QFont

class Demo(QWidget):
    def __init__(self):
        super(Demo, self).__init__()    

        self.datetime = QDateTimeEdit(QDate.currentDate())

        self.v_layout = QFormLayout(self)
        self.v_layout.addRow(QLabel('DD.MM.YYYY'), self.datetime)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setFont(QFont("Times", 12, QFont.Bold))
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM