- quint16 start_pos_x = 30;
- quint16 start_pos_y = 30;
- quint16 len_x = 775;
- quint16 len_y = 200;
- quint16 stop_pos_x = start_pos_x + len_x;
- quint16 stop_pos_y = start_pos_y + len_y;
- quint16 offset = 25;
- void MainWindow::paintEvent(QPaintEvent *)
- {
- QPainter painter(this); //实例画家对象
- painter.drawRect(QRect(start_pos_x,start_pos_y,len_x,len_y)); //画矩形
- //绘制水平均分线
- for(quint8 i=1;i<8;i++)
- {
- painter.drawLine(QPoint(start_pos_x,start_pos_y+offset*i),QPoint(stop_pos_x,start_pos_y+offset*i));
- }
- //绘制垂直均分线
- for(quint8 i=1;i<31;i++)
- {
- painter.drawLine(QPoint(start_pos_x+offset*i,start_pos_y),QPoint(start_pos_x+offset*i,stop_pos_y));
- }
- }
运行效果如下:
第二步:在规划的网格上显示数字
DrawCode函数用来在规划的网格上显示数字,p为绘画师指针,cnt为6个网格的,quint8 num。
- void MainWindow::DrawBrush(QPainter *p,quint8 hs,quint8 ls)
- {
- p->drawRect(QRect(30 + 25*(ls-1),30 + 25*(hs-1),25,25));
- }
- void MainWindow::DrawCode(QPainter *p,quint8 cnt,quint8 num)
- {
- quint8 index = 5;
- quint8 offset = 0;
- switch(cnt)
- {
- case 1: offset = (cnt -1)*index; break;
- case 2: offset = (cnt -1)*index-1; break;
- case 3: offset = (cnt -1)*index+1; break;
- case 4: offset = (cnt -1)*index; break;
- case 5: offset = (cnt -1)*index+2; break;
- case 6: offset = (cnt -1)*index+1; break;
- }
- switch(num)
- {
- case 0:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-2 3-4
- DrawBrush(p,3,2+offset);
- DrawBrush(p,3,4+offset);
- // 4-2 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,4+offset);
- // 5-2 5-4
- DrawBrush(p,5,2+offset);
- DrawBrush(p,5,4+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- case 1:
- // 2-2 2-3
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- // 3-3 4-3 5-3
- DrawBrush(p,3,3+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,5,3+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- case 2:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-2 3-4
- // DrawBrush(p,3,2+offset);
- DrawBrush(p,3,4+offset);
- // 4-2 4-3 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,4,4+offset);
- //5-2
- DrawBrush(p,5,2+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- case 3:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-4
- DrawBrush(p,3,4+offset);
- // 4-2 4-3 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,4,4+offset);
- //5-4
- DrawBrush(p,5,4+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- case 4:
- // 2-2 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,4+offset);
- // 3-2 3-4
- DrawBrush(p,3,2+offset);
- DrawBrush(p,3,4+offset);
- // 4-2 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,4,4+offset);
- // 5-2 5-3 5-4
- DrawBrush(p,5,4+offset);
- // 6-4
- DrawBrush(p,6,4+offset);
- break;
- case 5:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-2
- DrawBrush(p,3,2+offset);
- // 4-2 4-3 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,4,4+offset);
- // 5-4
- DrawBrush(p,5,4+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- case 6:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-2
- DrawBrush(p,3,2+offset);
- // 4-2 4-3 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,4,4+offset);
- // 5-2 5-4
- DrawBrush(p,5,2+offset);
- DrawBrush(p,5,4+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- case 7:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-4
- DrawBrush(p,3,4+offset);
- // 4-4
- DrawBrush(p,4,4+offset);
- // 5-4
- DrawBrush(p,5,4+offset);
- // 6-4
- DrawBrush(p,6,4+offset);
- break;
- case 8:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-2 3-4
- DrawBrush(p,3,2+offset);
- DrawBrush(p,3,4+offset);
- // 4-2 4-3 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,4,4+offset);
- // 5-2 5-4
- DrawBrush(p,5,2+offset);
- DrawBrush(p,5,4+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- case 9:
- // 2-2 2-3 2-4
- DrawBrush(p,2,2+offset);
- DrawBrush(p,2,3+offset);
- DrawBrush(p,2,4+offset);
- // 3-2 3-4
- DrawBrush(p,3,2+offset);
- DrawBrush(p,3,4+offset);
- // 4-2 4-3 4-4
- DrawBrush(p,4,2+offset);
- DrawBrush(p,4,3+offset);
- DrawBrush(p,4,4+offset);
- // 5-4
- DrawBrush(p,5,4+offset);
- // 6-2 6-3 6-4
- DrawBrush(p,6,2+offset);
- DrawBrush(p,6,3+offset);
- DrawBrush(p,6,4+offset);
- break;
- }
- }
第三步:获取时间,并在规划的网格中显示出来
- void MainWindow::DisplayTime(QPainter *painter)
- {
- QTime time = QTime::currentTime();
- QString timeStr = time.toString("hh:mm:ss");
- DrawCode(painter,1,timeStr.mid(0,2).toInt()/10);
- DrawCode(painter,2,timeStr.mid(0,2).toInt()%10);
- DrawCode(painter,3,timeStr.mid(3,2).toInt()/10);
- DrawCode(painter,4,timeStr.mid(3,2).toInt()%10);
- DrawCode(painter,5,timeStr.mid(6,2).toInt()/10);
- DrawCode(painter,6,timeStr.mid(6,2).toInt()%10);
- }
在paintEvent函数调用,由于未开启定时器,paintEvent函数只调用一次,只能显示固定的值,效果如下:
第四步:开启定时器同步刷新时间显示
添加定时器,每1S刷新一次界面,使用update调用paintEvent函数同步时间显示
- QTimer *timer0 = new QTimer(this);
- timer0->start(1000);
- connect(timer0,&QTimer::timeout,[=]()
- {
- update();
- });
最终效果如下: