当前位置:网站首页>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_H
The 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);
}
边栏推荐
- Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
- 7-22 tortoise and rabbit race (result oriented)
- tonybot 人形机器人 首次开机 0630
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- String substitution
- Doris学习笔记之数据表的创建
- MySQL multi table query subquery
- China PETG market forecast and Strategic Research Report (2022 Edition)
- Add ZABBIX calculation type itemcalculated items
- retrofit
猜你喜欢
论文分享:Generating Playful Palettes from Images
Puzzle (016.4) domino effect
Use of constraintlayout
Tonybot humanoid robot infrared remote control play 0630
Four data flows and cases of grpc
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Adc128s022 ADC Verilog design and Implementation
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
retrofit
随机推荐
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
7-3 rental (20 points)
数学常数表 by q779
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
7-20 print 99 formula table (format output)
To improve efficiency or increase costs, how should developers understand pair programming?
dllexport和dllimport
Talking about part of data storage in C language
分布式事务(Seata) 四大模式详解
String reverse order
Piwigo 2.7.1 sqli learning
X86 assembly language - Notes from real mode to protected mode
Zzuli:1042 sum of sequence 3
关于敏捷的一些概念
1017 a divided by B (20 points)
Zzuli:1046 product of odd numbers
pyQt界面制作(登录+跳转页面)
Zzuli:1056 lucky numbers
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
String substitution