当前位置:网站首页>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);
}边栏推荐
- 556. The next larger element III: simple construction simulation questions
- Convert string to decimal integer
- Adc128s022 ADC Verilog design and Implementation
- Talking about part of data storage in C language
- Tonybot humanoid robot starts for the first time 0630
- adc128s022 ADC verilog设计实现
- 7-1 positive integer a+b (15 points)
- 7-28 monkeys choose King (Joseph problem)
- 【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
- Zzuli:1049 square sum and cubic sum
猜你喜欢

tonybot 人形机器人 查看端口并对应端口 0701

Understand the application scenario and implementation mechanism of differential segment

ShowMeBug入驻腾讯会议,开启专业级技术面试时代

论文分享:Generating Playful Palettes from Images

表单文本框的使用(一) 选择文本

提高效率 Or 增加成本,开发人员应如何理解结对编程?

Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in

Dllexport et dllimport

亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?

dllexport和dllimport
随机推荐
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
Dllexport et dllimport
Common shortcut keys in PCB
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Output student grades
C language to implement a password manager (under update)
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
Zzuli:1043 max
Zzuli:1052 sum of sequence 4
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
NOI OPENJUDGE 1.6(09)
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
Zzuli:1057 prime number determination
adc128s022 ADC verilog设计实现
Thread. Sleep and timeunit SECONDS. The difference between sleep
Puzzle (016.3) is inextricably linked
Thread.sleep和TimeUnit.SECONDS.sleep的区别
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
Common commands for getting started with mongodb database
数学常数表 by q779