1、dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QRadioButton>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
enum ESinceEpochType {
EN_Secs = 0,
EN_MSecs,
};
void init();
void initLayout();
private slots:
void onConvertToSinceEpoch();
void onConvertToDtime();
private:
QLabel *m_lblYear = nullptr;
QLineEdit *m_edtYear = nullptr;
QLabel *m_lblMonth = nullptr;
QLineEdit *m_edtMonth = nullptr;
QLabel *m_lblDay = nullptr;
QLineEdit *m_edtDay = nullptr;
QLabel *m_lblHour = nullptr;
QLineEdit *m_edtHour = nullptr;
QLabel *m_lblMinute = nullptr;
QLineEdit *m_edtMinute = nullptr;
QLabel *m_lblSecond = nullptr;
QLineEdit *m_edtSecond = nullptr;
ESinceEpochType m_eSinceEpochType = EN_Secs;
QRadioButton *m_rbtnSecsSinceEpoch = nullptr;
QRadioButton *m_rbtnMSecsSinceEpoch = nullptr;
QPushButton *m_btnConvert = nullptr;
QLabel *m_lblTimestamp = nullptr;
QLineEdit *m_edtTimestamp = nullptr;
QPushButton *m_btnConvert2 = nullptr;
QLabel *m_lblDateTime = nullptr;
QLineEdit *m_edtDateTime = nullptr;
};
#endif // DIALOG_H
2、dialog.cpp
#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDateTime>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
init();
initLayout();
}
Dialog::~Dialog()
{
}
void Dialog::init()
{
m_lblYear = new QLabel("年", this);
m_edtYear = new QLineEdit("2022", this);
m_lblMonth = new QLabel("月", this);
m_edtMonth = new QLineEdit("12", this);
m_lblDay = new QLabel("日", this);
m_edtDay = new QLineEdit("28", this);
m_lblHour = new QLabel("时", this);
m_edtHour = new QLineEdit("14", this);
m_lblMinute = new QLabel("分", this);
m_edtMinute = new QLineEdit("43", this);
m_lblSecond = new QLabel("秒", this);
m_edtSecond = new QLineEdit("30", this);
m_rbtnSecsSinceEpoch = new QRadioButton("时间戳(秒)", this);
m_rbtnMSecsSinceEpoch = new QRadioButton("时间戳(毫秒)", this);
m_btnConvert = new QPushButton("转换为时间戳", this);
m_lblTimestamp = new QLabel("时间戳:", this);
m_edtTimestamp = new QLineEdit(this);
m_btnConvert2 = new QPushButton("转换为日期", this);
m_lblDateTime = new QLabel("日期:", this);
m_edtDateTime = new QLineEdit(this);
m_edtYear->setFixedSize(100, 30);
m_edtMonth->setFixedSize(100, 30);
m_edtDay->setFixedSize(100, 30);
m_edtHour->setFixedSize(100, 30);
m_edtMinute->setFixedSize(100, 30);
m_edtSecond->setFixedSize(100, 30);
m_btnConvert->setFixedSize(100, 30);
m_edtTimestamp->setFixedSize(200, 30);
m_btnConvert2->setFixedSize(100, 30);
m_edtDateTime->setFixedSize(200, 30);
m_edtDateTime->setEnabled(false);
m_rbtnSecsSinceEpoch->setChecked(true);
m_rbtnMSecsSinceEpoch->setChecked(false);
m_eSinceEpochType = EN_Secs;
connect(m_rbtnSecsSinceEpoch, &QRadioButton::toggled, this, [this] {
m_eSinceEpochType = EN_Secs;
onConvertToSinceEpoch();
onConvertToDtime();
});
connect(m_rbtnMSecsSinceEpoch, &QRadioButton::toggled, this, [this] {
m_eSinceEpochType = EN_MSecs;
onConvertToSinceEpoch();
onConvertToDtime();
});
connect(m_btnConvert, &QPushButton::clicked, this, &Dialog::onConvertToSinceEpoch);
connect(m_btnConvert2, &QPushButton::clicked, this, &Dialog::onConvertToDtime);
}
void Dialog::initLayout()
{
QHBoxLayout *hLay1 = new QHBoxLayout();
hLay1->setMargin(0);
hLay1->setSpacing(16);
hLay1->addWidget(m_edtYear);
hLay1->addWidget(m_lblYear);
hLay1->addWidget(m_edtMonth);
hLay1->addWidget(m_lblMonth);
hLay1->addWidget(m_edtDay);
hLay1->addWidget(m_lblDay);
QHBoxLayout *hLay2 = new QHBoxLayout();
hLay2->setMargin(0);
hLay2->setSpacing(16);
hLay2->addWidget(m_edtHour);
hLay2->addWidget(m_lblHour);
hLay2->addWidget(m_edtMinute);
hLay2->addWidget(m_lblMinute);
hLay2->addWidget(m_edtSecond);
hLay2->addWidget(m_lblSecond);
QHBoxLayout *hLayRbtn = new QHBoxLayout();
hLayRbtn->setMargin(0);
hLayRbtn->setSpacing(20);
hLayRbtn->addStretch(0);
hLayRbtn->addWidget(m_rbtnSecsSinceEpoch);
hLayRbtn->addWidget(m_rbtnMSecsSinceEpoch);
hLayRbtn->addStretch(0);
QHBoxLayout *hLay3 = new QHBoxLayout();
hLay3->setMargin(0);
hLay3->setSpacing(16);
hLay3->addWidget(m_btnConvert);
hLay3->addStretch();
hLay3->addWidget(m_lblTimestamp);
hLay3->addWidget(m_edtTimestamp);
QHBoxLayout *hLay4 = new QHBoxLayout();
hLay4->setMargin(0);
hLay4->setSpacing(16);
hLay4->addWidget(m_btnConvert2);
hLay4->addStretch();
hLay4->addWidget(m_lblDateTime);
hLay4->addWidget(m_edtDateTime);
QVBoxLayout *vLayMain = new QVBoxLayout(this);
vLayMain->setMargin(60);
vLayMain->setSpacing(20);
vLayMain->addLayout(hLay1);
vLayMain->addLayout(hLay2);
vLayMain->addLayout(hLayRbtn);
vLayMain->addLayout(hLay3);
vLayMain->addLayout(hLay4);
this->setLayout(vLayMain);
}
void Dialog::onConvertToSinceEpoch()
{
QString strDtime = QString("%1-%2-%3 %4:%5:%6")
.arg(m_edtYear->text(), m_edtMonth->text(), m_edtDay->text(),
m_edtHour->text(), m_edtMinute->text(), m_edtSecond->text());
QDateTime dt = QDateTime::fromString(strDtime, "yyyy-MM-dd hh:mm:ss");
if (dt.isValid()) {
qint64 timeStamp;
if (EN_Secs == m_eSinceEpochType)
timeStamp = dt.toSecsSinceEpoch();
else if (EN_MSecs == m_eSinceEpochType)
timeStamp = dt.toMSecsSinceEpoch();
else
timeStamp = 0;
m_edtTimestamp->setText(QString::number(timeStamp));
}
}
void Dialog::onConvertToDtime()
{
QDateTime dt;
if (EN_Secs == m_eSinceEpochType)
dt = QDateTime::fromSecsSinceEpoch(m_edtTimestamp->text().toLongLong());
else if (EN_MSecs == m_eSinceEpochType)
dt = QDateTime::fromMSecsSinceEpoch(m_edtTimestamp->text().toLongLong());
if (dt.isValid())
m_edtDateTime->setText(dt.toString("yyyy-MM-dd hh:mm:ss"));
}
3、运行界面:文章来源:https://www.toymoban.com/news/detail-632973.html
文章来源地址https://www.toymoban.com/news/detail-632973.html
到了这里,关于Qt:日期与时间戳互相转换Demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!