[程序源码] 基于QT矩阵时钟设计

[复制链接]
 楼主| 一路向北lm 发表于 2023-6-15 21:49 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2023-6-15 21:54 编辑

第一步:规划网格
使用qt的paintEvent函数绘制8*31网格,网格的起始点坐标为(30,30),长775,宽200,每一个方格的长度为25。
  1. quint16 start_pos_x = 30;
  2. quint16 start_pos_y = 30;
  3. quint16 len_x = 775;
  4. quint16 len_y = 200;
  5. quint16 stop_pos_x = start_pos_x + len_x;
  6. quint16 stop_pos_y = start_pos_y + len_y;
  7. quint16 offset = 25;
  8. void MainWindow::paintEvent(QPaintEvent *)
  9. {
  10.     QPainter painter(this);  //实例画家对象
  11.     painter.drawRect(QRect(start_pos_x,start_pos_y,len_x,len_y));  //画矩形
  12.     //绘制水平均分线
  13.     for(quint8 i=1;i<8;i++)
  14.     {
  15.       painter.drawLine(QPoint(start_pos_x,start_pos_y+offset*i),QPoint(stop_pos_x,start_pos_y+offset*i));
  16.     }
  17.     //绘制垂直均分线
  18.     for(quint8 i=1;i<31;i++)
  19.     {
  20.       painter.drawLine(QPoint(start_pos_x+offset*i,start_pos_y),QPoint(start_pos_x+offset*i,stop_pos_y));
  21. }
  22. }
运行效果如下:
第二步:在规划的网格上显示数字
DrawCode函数用来在规划的网格上显示数字,p为绘画师指针,cnt为6个网格的,quint8 num。
  1. void MainWindow::DrawBrush(QPainter *p,quint8 hs,quint8 ls)
  2. {
  3.     p->drawRect(QRect(30 + 25*(ls-1),30 + 25*(hs-1),25,25));
  4. }

  5. void MainWindow::DrawCode(QPainter *p,quint8 cnt,quint8 num)
  6. {
  7.     quint8 index = 5;
  8.     quint8 offset = 0;
  9.     switch(cnt)
  10.     {
  11.         case 1:  offset = (cnt -1)*index; break;
  12.         case 2:  offset = (cnt -1)*index-1; break;
  13.         case 3:  offset = (cnt -1)*index+1; break;
  14.         case 4:  offset = (cnt -1)*index; break;
  15.         case 5:  offset = (cnt -1)*index+2; break;
  16.         case 6:  offset = (cnt -1)*index+1; break;
  17.     }
  18.     switch(num)
  19.     {
  20.         case 0:
  21.                 // 2-2 2-3 2-4
  22.                 DrawBrush(p,2,2+offset);
  23.                 DrawBrush(p,2,3+offset);
  24.                 DrawBrush(p,2,4+offset);

  25.                 // 3-2 3-4
  26.                 DrawBrush(p,3,2+offset);
  27.                 DrawBrush(p,3,4+offset);

  28.                 // 4-2 4-4
  29.                 DrawBrush(p,4,2+offset);
  30.                 DrawBrush(p,4,4+offset);

  31.                 // 5-2 5-4
  32.                 DrawBrush(p,5,2+offset);
  33.                 DrawBrush(p,5,4+offset);
  34.                 // 6-2 6-3 6-4
  35.                 DrawBrush(p,6,2+offset);
  36.                 DrawBrush(p,6,3+offset);
  37.                 DrawBrush(p,6,4+offset);

  38.         break;
  39.       case 1:
  40.                 // 2-2 2-3
  41.                 DrawBrush(p,2,2+offset);
  42.                 DrawBrush(p,2,3+offset);
  43.                 // 3-3 4-3  5-3
  44.                 DrawBrush(p,3,3+offset);
  45.                 DrawBrush(p,4,3+offset);
  46.                 DrawBrush(p,5,3+offset);
  47.                 // 6-2 6-3 6-4
  48.                 DrawBrush(p,6,2+offset);
  49.                 DrawBrush(p,6,3+offset);
  50.                 DrawBrush(p,6,4+offset);
  51.       break;
  52.       case 2:
  53.                 // 2-2 2-3 2-4
  54.                 DrawBrush(p,2,2+offset);
  55.                 DrawBrush(p,2,3+offset);
  56.                 DrawBrush(p,2,4+offset);

  57.                 // 3-2 3-4
  58.                // DrawBrush(p,3,2+offset);
  59.                 DrawBrush(p,3,4+offset);

  60.                 // 4-2 4-3 4-4
  61.                 DrawBrush(p,4,2+offset);
  62.                 DrawBrush(p,4,3+offset);
  63.                 DrawBrush(p,4,4+offset);

  64.                 //5-2
  65.                 DrawBrush(p,5,2+offset);

  66.                 // 6-2 6-3 6-4
  67.                 DrawBrush(p,6,2+offset);
  68.                 DrawBrush(p,6,3+offset);
  69.                 DrawBrush(p,6,4+offset);
  70.       break;
  71.       case 3:
  72.               // 2-2 2-3 2-4
  73.               DrawBrush(p,2,2+offset);
  74.               DrawBrush(p,2,3+offset);
  75.               DrawBrush(p,2,4+offset);

  76.               // 3-4
  77.               DrawBrush(p,3,4+offset);

  78.               // 4-2 4-3 4-4
  79.               DrawBrush(p,4,2+offset);
  80.               DrawBrush(p,4,3+offset);
  81.               DrawBrush(p,4,4+offset);

  82.               //5-4
  83.               DrawBrush(p,5,4+offset);

  84.               // 6-2 6-3 6-4
  85.               DrawBrush(p,6,2+offset);
  86.               DrawBrush(p,6,3+offset);
  87.               DrawBrush(p,6,4+offset);
  88.       break;
  89.         case 4:
  90.                 // 2-2 2-4
  91.                 DrawBrush(p,2,2+offset);
  92.                 DrawBrush(p,2,4+offset);

  93.                 // 3-2 3-4
  94.                 DrawBrush(p,3,2+offset);
  95.                 DrawBrush(p,3,4+offset);

  96.                 // 4-2 4-4
  97.                 DrawBrush(p,4,2+offset);
  98.                 DrawBrush(p,4,3+offset);
  99.                 DrawBrush(p,4,4+offset);

  100.                 // 5-2 5-3 5-4
  101.                 DrawBrush(p,5,4+offset);
  102.                 // 6-4
  103.                 DrawBrush(p,6,4+offset);
  104.         break;
  105.         case 5:
  106.                 // 2-2 2-3 2-4
  107.                 DrawBrush(p,2,2+offset);
  108.                 DrawBrush(p,2,3+offset);
  109.                 DrawBrush(p,2,4+offset);

  110.                 // 3-2
  111.                 DrawBrush(p,3,2+offset);

  112.                 // 4-2 4-3 4-4
  113.                 DrawBrush(p,4,2+offset);
  114.                 DrawBrush(p,4,3+offset);
  115.                 DrawBrush(p,4,4+offset);

  116.                 // 5-4
  117.                 DrawBrush(p,5,4+offset);

  118.                 // 6-2 6-3 6-4
  119.                 DrawBrush(p,6,2+offset);
  120.                 DrawBrush(p,6,3+offset);
  121.                 DrawBrush(p,6,4+offset);

  122.         break;
  123.     case 6:
  124.             // 2-2 2-3 2-4
  125.             DrawBrush(p,2,2+offset);
  126.             DrawBrush(p,2,3+offset);
  127.             DrawBrush(p,2,4+offset);

  128.             // 3-2
  129.             DrawBrush(p,3,2+offset);

  130.             // 4-2 4-3 4-4
  131.             DrawBrush(p,4,2+offset);
  132.             DrawBrush(p,4,3+offset);
  133.             DrawBrush(p,4,4+offset);

  134.             // 5-2 5-4
  135.             DrawBrush(p,5,2+offset);
  136.             DrawBrush(p,5,4+offset);
  137.             // 6-2 6-3 6-4
  138.             DrawBrush(p,6,2+offset);
  139.             DrawBrush(p,6,3+offset);
  140.             DrawBrush(p,6,4+offset);

  141.     break;
  142.     case 7:
  143.             // 2-2 2-3 2-4
  144.             DrawBrush(p,2,2+offset);
  145.             DrawBrush(p,2,3+offset);
  146.             DrawBrush(p,2,4+offset);

  147.             // 3-4
  148.             DrawBrush(p,3,4+offset);

  149.             // 4-4
  150.             DrawBrush(p,4,4+offset);

  151.             // 5-4
  152.             DrawBrush(p,5,4+offset);
  153.             // 6-4
  154.             DrawBrush(p,6,4+offset);

  155.     break;
  156.     case 8:
  157.             // 2-2 2-3 2-4
  158.             DrawBrush(p,2,2+offset);
  159.             DrawBrush(p,2,3+offset);
  160.             DrawBrush(p,2,4+offset);

  161.             // 3-2 3-4
  162.             DrawBrush(p,3,2+offset);
  163.             DrawBrush(p,3,4+offset);

  164.             // 4-2 4-3 4-4
  165.             DrawBrush(p,4,2+offset);
  166.             DrawBrush(p,4,3+offset);
  167.             DrawBrush(p,4,4+offset);

  168.             // 5-2 5-4
  169.             DrawBrush(p,5,2+offset);
  170.             DrawBrush(p,5,4+offset);
  171.             // 6-2 6-3 6-4
  172.             DrawBrush(p,6,2+offset);
  173.             DrawBrush(p,6,3+offset);
  174.             DrawBrush(p,6,4+offset);

  175.     break;
  176.     case 9:
  177.             // 2-2 2-3 2-4
  178.             DrawBrush(p,2,2+offset);
  179.             DrawBrush(p,2,3+offset);
  180.             DrawBrush(p,2,4+offset);

  181.             // 3-2 3-4
  182.             DrawBrush(p,3,2+offset);
  183.             DrawBrush(p,3,4+offset);

  184.             // 4-2 4-3 4-4
  185.             DrawBrush(p,4,2+offset);
  186.             DrawBrush(p,4,3+offset);
  187.             DrawBrush(p,4,4+offset);

  188.             // 5-4
  189.             DrawBrush(p,5,4+offset);
  190.             // 6-2 6-3 6-4
  191.             DrawBrush(p,6,2+offset);
  192.             DrawBrush(p,6,3+offset);
  193.             DrawBrush(p,6,4+offset);

  194.     break;

  195.     }
  196. }
第三步:获取时间,并在规划的网格中显示出来
  1. void  MainWindow::DisplayTime(QPainter *painter)
  2. {
  3.     QTime time = QTime::currentTime();
  4.     QString timeStr = time.toString("hh:mm:ss");

  5.     DrawCode(painter,1,timeStr.mid(0,2).toInt()/10);
  6.     DrawCode(painter,2,timeStr.mid(0,2).toInt()%10);

  7.     DrawCode(painter,3,timeStr.mid(3,2).toInt()/10);
  8.     DrawCode(painter,4,timeStr.mid(3,2).toInt()%10);

  9.     DrawCode(painter,5,timeStr.mid(6,2).toInt()/10);
  10.     DrawCode(painter,6,timeStr.mid(6,2).toInt()%10);

  11. }
在paintEvent函数调用,由于未开启定时器,paintEvent函数只调用一次,只能显示固定的值,效果如下:
第四步:开启定时器同步刷新时间显示
添加定时器,每1S刷新一次界面,使用update调用paintEvent函数同步时间显示
  1. QTimer *timer0  = new QTimer(this);
  2. timer0->start(1000);
  3.     connect(timer0,&QTimer::timeout,[=]()
  4.     {
  5.         update();
  6.     });
最终效果如下:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3838

帖子

81

粉丝
快速回复 返回顶部 返回列表