我认为最后再写个分支语句,就能实现你说的功能~先把两个时间加载到QDateTime中,然后分别对其中的每一项进行相减。还要麻烦些,还需要一个QTime类,依法而行。——首先以空格为界限拆分字符串,QDateTime time1 = QDateTime::fromString("2012-07-23 15:30:20", "yyyy-MM-dd hh:mm:ss");参考下:C/C++ code QTime time1 = QTime::fromString("15:35:20","hh:mm:ss"); QTime time2 = QTime::fromString("15:30:20","hh:mm:ss"); qDebug()
主要代码如下,其他的根据需要自己调整吧。
qlabelsampleh文件
#ifndef QLABELSAMPLE_H#define QLABELSAMPLE_H
#include <QDialog>
#include <QTimer>
#include <QDateTime>
namespace Ui {
class QLabelSample;
}
class QLabelSample : public QDialog
{
Q_OBJECT
public:
explicit QLabelSample(QWidget parent = 0);
~QLabelSample();
private slots:
void updateLabelTime(void);
private:
Ui::QLabelSample ui;
QTimer m_timer;
};
#endif // QLABELSAMPLE_H
qlabelsamplecpp文件
#include "qlabelsampleh"#include "ui_qlabelsampleh"
QLabelSample::QLabelSample(QWidget parent) :
QDialog(parent),
ui(new Ui::QLabelSample),
m_timer(this)
{
ui->setupUi(this);
QFont SimSunFont("SimSun", 16);
ui->label->setFont(SimSunFont);
ui->label->setStyleSheet("color:blue; background-color:red");
m_timersetTimerType(Qt::PreciseTimer);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateLabelTime()));
m_timerstart(100);
}
QLabelSample::~QLabelSample()
{
delete ui;
}
void QLabelSample::updateLabelTime(void)
{
ui->label->setText(QDateTime::currentDateTime()toString(Qt::DefaultLocaleLongDate));
}
以上就是关于Qt 日期时间如何相加和相减全部的内容,包括:Qt 日期时间如何相加和相减、qt 利用QTimer定时器和QLabel显示系统时间,将标签字体设置成16像素高,标签背景设置、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)