Qt开发——电子时钟软件

目录less

效果图函数

以前pyqt5作过相似的:this

思路:QMouseEvent+QLCDNum,ber+QTime+QTimer.net

digiclock.cpp调试

digiclock.hcode

main.cpp对象

调试记录:blog


效果图

以前pyqt5作过相似的:

http://www.javashuo.com/article/p-sclduqqt-go.htmlci

思路:QMouseEvent+QLCDNum,ber+QTime+QTimer

digiclock.cpp

#include "digiclock.h"
#include <QTimer>
#include <QTime>
#include <QMouseEvent>

DigiClock::DigiClock(QWidget *parent):QLCDNumber(parent){

    //设置时钟背景
    QPalette p=palette();
    p.setColor(QPalette::Window,Qt::black);
    setPalette(p);
    setWindowFlag(Qt::FramelessWindowHint);//设置窗体标识,设置为没有面板边框和标题栏的窗体
    setWindowOpacity(0.5);//设置窗体透明度为0.5,即半透明

    QTimer *timer=new QTimer(this);//新建一个定时器对象
    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));//链接信号与槽
    timer->start(1000);//以1000毫秒为周期启动定时器
    showTime();//初始化时间显示
    resize(450,200);//设置时钟尺寸
    showColon=true;//闪烁标志位初始化

}

//显示时间
void DigiClock::showTime(){
    printf("connect is ok\n");
    QTime time = QTime::currentTime();//获取当前系统时间,保存在一个新对象中
    QString text = time.toString("hh:mm");
    //闪烁功能
    if(showColon){
        text[2]=':';
        showColon=false;
        printf("#33");
    }else{
        text[2]=' ';
        showColon=true;
        printf("#37");
    }
    display(text);//显示转换好的字符串时间
}

//判断鼠标是否按下状态函数
void DigiClock::mousePressEvent(QMouseEvent *event){
    if(event->buttons()==Qt::LeftButton){//若是是左键按下
        dragPosition=event->globalPos()-frameGeometry().topLeft();//获取鼠标相对于窗体左上角的偏移值
        event->accept();
    }
    if(event->buttons()==Qt::RightButton){//若是右键按下
        printf("no!\n");
        close();//若是鼠标右键按下则关闭窗口
    }

}

//鼠标响应函数
void DigiClock::mouseMoveEvent(QMouseEvent *event){
    if(event->buttons()&Qt::LeftButton){//若是鼠标左键按下
//        printf("moving!");
        move(event->globalPos()-dragPosition);//移动窗口
        event->accept();
    }
}

digiclock.h

#ifndef DIGICLOCK_H
#define DIGICLOCK_H

#include <QLCDNumber>
#include <QTimer>
#include <QTime>
#include <QMouseEvent>

class DigiClock : public QLCDNumber
{
public:
    DigiClock(QWidget *parent=nullptr);
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);

private:
    QPoint dragPosition;//保存鼠标相对于电子时钟窗体左上角的偏移值
    bool showColon;//用于显示时是否显示":"的标志位

private slots:
    void showTime();//显示当前时间

};

#endif // DIGICLOCK_H

main.cpp

//#include "dialog.h"
#include "digiclock.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    DigiClock clock;
    clock.show();

    return a.exec();
}

调试记录:

event->buttons()这句一开始写成event->button()会致使程序阻塞,这个bug我找了半天才发现gg

冒号闪烁问题一直没解决。。。怀疑是信号有问题,可是按照手册来没发现毛病。。。调试的时候信号计时确实有问题。。。timer有毛病字符串