QPushButton 制作电子时钟

             最近在学习Qt,从网上看到一些方法,用QPushButton写了电子时钟。主要是利用QPushButton类的setIcon(QIcon(filename))方法。

        效果如下:

 

         准备显示0-9数字的png格式图片,并以对应的数字命名,这样就可以通过数字很快的找到图片,并通过QPushButton显示出来。

         直接上代码:

#ifndef CLOCK_H
#define CLOCK_H

#include <QTime>
#include <QTimer>
#include <QPoint>
#include <QPixmap>
#include <QMouseEvent>
#include <QMainWindow>

namespace Ui{
   class cl;
}
class Clock :public QMainWindow
{   Q_OBJECT
public:
    Clock(QWidget *parent=0);
    QString getPngName(QChar);
protected:
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
private:
    Ui::cl *ui;
    QTimer *timer;
    QPoint dpos;
private slots:
    void showTime();//显示时间
};

#endif // CLOCK_H
 
#include "clock.h"
#include "ui_cl.h"

Clock::Clock(QWidget *parent)
    :QMainWindow(parent,Qt::FramelessWindowHint),
      ui(new Ui::cl)
{   ui->setupUi(this);
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
    timer->start(1000);
    ui->g->setIcon((QIcon(":/dot.png")));
    ui->h->setIcon((QIcon(":/dot.png")));
    connect(ui->f,SIGNAL(clicked()),this,SLOT(close()));
    showTime();
}


QString Clock::getPngName(QChar x){
    return QString(":/")+x+QString(".png");
}


void Clock::showTime(){
    QTime time=QTime::currentTime();
    QString text=time.toString("hh:mm:ss");
    ui->a->setIcon(QIcon(getPngName(text[0])));
    ui->b->setIcon(QIcon(getPngName(text[1])));
    ui->c->setIcon(QIcon(getPngName(text[3])));
    ui->d->setIcon(QIcon(getPngName(text[4])));
    ui->e->setIcon(QIcon(getPngName(text[6])));
    ui->f->setIcon(QIcon(getPngName(text[7])));
}

void Clock::mousePressEvent(QMouseEvent *e){
    dpos=e->globalPos()-frameGeometry().topLeft();
}

void Clock::mouseMoveEvent(QMouseEvent *e){
    if(e->buttons()&Qt::LeftButton){
        move(e->globalPos()-dpos);
        e->accept();
    }
}


#include<QApplication>
#include"clock.h"
int main(int argc,char* argv[]){
    QApplication app(argc,argv);
    Clock w;
    w.setWindowOpacity(1);
    w.setWindowFlags(Qt::FramelessWindowHint);
    w.setAttribute(Qt::WA_TranslucentBackground);
    w.show();
    return app.exec();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中创建一个电子时钟UI界面,通常会涉及到使用QLabel来显示时间,QDateTimeEdit或者QTimer来更新时间,并可能用到QPushButton或其他控件来启动或暂停计时。以下是一个简单的步骤概述: 1. 导入所需库: ```cpp #include <QApplication> #include <QWidget> #include <QLabel> #include <QDateTimeEdit> #include <QPushButton> #include <QTimer> ``` 2. 设计窗口类并继承自QWidget: ```cpp class TimeDisplayWidget : public QWidget { Q_OBJECT public: TimeDisplayWidget(QWidget *parent = nullptr); ~TimeDisplayWidget(); private slots: void updateTime(); private: QLabel *timeLabel; QDateTimeEdit *timeEdit; QPushButton *startStopButton; QTimer *timer; }; ``` 3. 实现构造函数和槽函数: ```cpp TimeDisplayWidget::TimeDisplayWidget(QWidget *parent) : QWidget(parent) { // 设置窗口布局 QVBoxLayout *layout = new QVBoxLayout(this); // 创建时间标签 timeLabel = new QLabel(QStringLiteral("00:00:00")); layout->addWidget(timeLabel); // 创建时间编辑框,主要用于设置初始时间 timeEdit = new QDateTimeEdit(this); layout->addWidget(timeEdit); // 创建开始/停止按钮 startStopButton = new QPushButton(QStringLiteral("Start"), this); startStopButton->setFixedSize(75, 30); layout->addWidget(startStopButton); // 设置定时器 timer = new QTimer(this); connect(startStopButton, &QPushButton::clicked, this, &TimeDisplayWidget::toggleTimer); connect(timer, &QTimer::timeout, this, &TimeDisplayWidget::updateTime); } void TimeDisplayWidget::updateTime() { QDateTime currentDateTime = QDateTime::currentDateTime(); timeLabel->setText(currentDateTime.toString(QStringLiteral("hh:mm:ss"))); } void TimeDisplayWidget::toggleTimer() { if (timer->isActive()) { timer->stop(); startStopButton->setText(QStringLiteral("Start")); } else { timer->start(1000); // 每秒更新一次 startStopButton->setText(QStringLiteral("Stop")); } } ``` 4. 主函数: ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); TimeDisplayWidget window; window.show(); return app.exec(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值