【Qt】QLabel实时显示系统时间

要在标签上实时显示系统时间,首先要获取系统时间,然后运用定时器每一秒更新一次时间;

效果:

具体步骤如下:

1.创建QDateTime对象获取当前日期和时间

 dateTime =QDateTime::currentDateTime();//获取系统时间

2.创建QLabel显示时间

showCurrentTime = new QLabel(dateTime.toString("yyyy-MM-dd hh:mm:ss"),this);

3.创建定时器定时更新时间和日期

    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timeUpdate()));//连接信号槽
    timer->start(1000);//1s更新一次

槽函数中获取当前时间,并进行更新;时间每1s更新一次

void Widget::timeUpdate()
{
    dateTime =QDateTime::currentDateTime();//获取当前系统时间
    showCurrentTime->setText(dateTime.toString("yyyy-MM-dd hh:mm:ss"));
}

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Qt的QWidget中实时显示并绘制数据折线图,可以参考以下步骤: 1. 在QWidget中添加一个QCustomPlot控件用于绘制折线图。 2. 在QWidget的构造函数中,创建一个Qtimer对象,用于定时更新数据。 3. 在Qtimer的槽函数中,读取数据并更新折线图。 4. 在QCustomPlot控件的初始化函数中,设置x和y轴范围、坐标轴标签等属性。 5. 在更新折线图的槽函数中,先将新的数据添加到QVector容器中,再将容器中的数据绘制成折线图。 下面是一个简单的代码示例,仅供参考: ``` // QWidget类 class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); ~MyWidget(); private slots: void updateData(); // 定时更新数据的槽函数 void plotGraph(); // 绘制折线图的槽函数 private: QCustomPlot *m_customPlot; // 绘制折线图的QCustomPlot控件 QTimer *m_timer; // 定时器对象 QVector<double> m_xData, m_yData; // 数据容器 }; // 构造函数 MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { // 创建QCustomPlot控件 m_customPlot = new QCustomPlot(this); // 创建定时器对象 m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(updateData())); m_timer->start(100); // 每100ms更新一次数据 // 初始化QCustomPlot控件 m_customPlot->addGraph(); m_customPlot->graph(0)->setPen(QPen(Qt::blue)); m_customPlot->xAxis->setLabel("Time"); m_customPlot->yAxis->setLabel("Value"); m_customPlot->xAxis->setRange(0, 10); m_customPlot->yAxis->setRange(0, 100); // 设置布局 QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(m_customPlot); setLayout(layout); } // 析构函数 MyWidget::~MyWidget() { } // 定时更新数据的槽函数 void MyWidget::updateData() { // 读取数据... // 处理数据... plotGraph(); // 更新折线图 } // 绘制折线图的槽函数 void MyWidget::plotGraph() { double x = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0; // 获取当前时间 double y = 50; // 假设读取到的数据为50 m_xData.append(x); m_yData.append(y); // 绘制折线图 m_customPlot->graph(0)->setData(m_xData, m_yData); m_customPlot->replot(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

logani

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值