当前位置:网站首页>My QT learning path -- how qdatetimeedit is empty
My QT learning path -- how qdatetimeedit is empty
2022-07-03 14:40:00 【Caixiaoyi】
Study Qt It's been a few months , Gradually fell in love Qt, To record Qt Problems encountered in development :
QDatetimeEdit Is a commonly used date editing control , But the official control cannot be displayed as null . There is a new requirement in the recent test. We hope that the date editing control defaults to null , This is a little strange for a newcomer , But considering that I'm still in internship, I don't think much of it , Can only bear in silence . Although it doesn't sound difficult , But it still took a lot of time to realize , Hope to help students with similar needs .

QDateTimeEdit The default display of

Click the up or down arrow to display
The following is a .h Content of header file
#ifndef BLANKDATETIMEEDIT_H
#define BLANKDATETIMEEDIT_H
#include <QDateTimeEdit>
#include <QLineEdit>
#include <QDebug>
class BlankDateTimeEdit : public QDateTimeEdit
{
Q_OBJECT
public:
explicit BlankDateTimeEdit(QWidget *parent = 0);
public:
void SetNull(bool NullSwitch);// Setting is allowed to be empty
protected:
virtual QValidator::State validate(QString &input, int &pos) const override;
virtual QString textFromDateTime(const QDateTime &dt) const override;
void focusInEvent(QFocusEvent *e)override;// Rewrite the get focus function , Make it display time normally when it gets focus
void mousePressEvent(QMouseEvent *e)override;// Override mouse down function
private:
bool m_NullSwitch;// Is it allowed to be empty
};
#endif // BLANKDATETIMEEDIT_HThe following is a .cpp Specific implementation of file function
#include "blankdatetimeedit.h"
BlankDateTimeEdit::BlankDateTimeEdit(QWidget *parent) :
QDateTimeEdit(parent),m_NullSwitch(true)
{
this->setParent(parent);
lineEdit()->clear();
}
void BlankDateTimeEdit::SetNull(bool NullSwitch)
{
m_NullSwitch=NullSwitch;
if(m_NullSwitch)
{
lineEdit()->clear();
}
else
{
this->setDateTime(QDateTime::currentDateTime());
}
}
//QDateTime Call this virtual function to determine whether the input is valid ,pos The parameter represents the position in the string
QValidator::State BlankDateTimeEdit::validate(QString &input, int &pos) const
{
qDebug()<<"validate"<<input<<" "<<pos;
if(m_NullSwitch)
{
input="";
return QValidator::Intermediate;
}
return QDateTimeEdit::validate(input,pos);
}
QString BlankDateTimeEdit::textFromDateTime(const QDateTime &dt) const
{
return QDateTimeEdit::textFromDateTime(dt);
}
// It is used to get focus and pop-up keyboard in embedded , And display normally
// Note that there is only one control on the page , This control will get the focus when the page is initially displayed , Normal display
void BlankDateTimeEdit::focusInEvent(QFocusEvent *e)
{
if(0 == this->text().length())
{
// SetNull(false);
return;
}
QDateTimeEdit::focusInEvent(e);
}
void BlankDateTimeEdit::mousePressEvent(QMouseEvent *e)
{
if(0 == this->text().length())
{
SetNull(false);
return;
}
QDateTimeEdit::mousePressEvent(e);
}边栏推荐
- Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
- Find specified characters
- 7-19 check denomination (solve binary linear equation)
- Stop asking yourself if you are suitable for software testing
- tonybot 人形机器人 首次开机 0630
- String reverse order
- Luogu p3065 [usaco12dec]first! G problem solution
- Tonybot humanoid robot checks the port and corresponds to port 0701
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- Paper sharing: generating playful palettes from images
猜你喜欢

Detailed explanation of four modes of distributed transaction (Seata)

Creation of data table of Doris' learning notes

分布式事务(Seata) 四大模式详解

Dllexport et dllimport

论文分享:Generating Playful Palettes from Images

puzzle(016.4)多米诺效应

Use of form text box (I) select text

How to query the baby category of tmall on Taobao

Paper sharing: generating playful palettes from images

Bucket sorting in C language
随机推荐
Piwigo 2.7.1 sqli learning
NPM install is stuck with various strange errors of node NPY
NOI OPENJUDGE 1.5(23)
Find the sum of the elements of each row of the matrix
Plane vector addition
tonybot 人形机器人 首次开机 0630
Preliminary summary of structure
The mail function of LNMP environment cannot send mail
Puzzle (016.4) domino effect
Detailed explanation of four modes of distributed transaction (Seata)
String sort
Implement Gobang with C language
Address book sorting
Four data flows and cases of grpc
Adc128s022 ADC Verilog design and Implementation
Common shortcut keys in PCB
puzzle(016.4)多米诺效应
7-3 rental (20 points)
Zzuli:1059 highest score
Etcd cluster permission management and account password usage