TA的每日心情 | 奋斗 前天 18:23 |
---|
签到天数: 213 天 连续签到: 6 天 [LV.7]常住居民III
|
这次使用QT开发一个在开发板上运行的图形界面程序,用于显示系统时钟。
QT下新建一个dialog的项目,然后在头文件dialog.h中编写如下内容:
- #ifndef DIALOG_H
- #define DIALOG_H
- #include <QDialog>
- #include <QPainter>
- #include <QPen>
- #include <QColor>
- #include <QPointF>
- #include <QtMath>
- #include <QDateTime>
- #include <QTime>
- QT_BEGIN_NAMESPACE
- namespace Ui { class Dialog; }
- QT_END_NAMESPACE
- class Dialog : public QDialog
- {
- Q_OBJECT
- public:
- Dialog(QWidget *parent = nullptr);
- ~Dialog();
- protected:
- void paintEvent(QPaintEvent *event) override;
- void resizeEvent(QResizeEvent *event) override;
- void timerEvent(QTimerEvent *event) override;
- private:
- Ui::Dialog *ui;
- double SL=500;
- //hour pointer
- QColor ch=QColor(255,0,0);
- QPen ph=QPen(ch,8);
- //minute pointer
- QColor cm=QColor(0,255,0);
- QPen pm=QPen(cm,4);
- //second pointer
- QColor cs=QColor(0,0,255);
- QPen ps=QPen(cs,2);
- QColor cL=QColor(255,0,255);
- QPen pL=QPen(cL,3);
- QColor cW=QColor(0,128,0);
- QPen pW=QPen(cW,5);
- };
- #endif // DIALOG_H
复制代码 然后是源文件dialog.c中:
- #include "dialog.h"
- #include "ui_dialog.h"
- #include "QDebug"
- #include <QResizeEvent>
- Dialog::Dialog(QWidget *parent)
- : QDialog(parent)
- , ui(new Ui::Dialog)
- {
- ui->setupUi(this);
- setMinimumSize(500,500);
- resize(SL,SL);
- startTimer(499);
- }
- Dialog::~Dialog()
- {
- delete ui;
- }
- void Dialog::paintEvent(QPaintEvent *event)
- {
- Q_UNUSED(event)
- QPainter painter(this);
- QPoint pc(width()/2,height()/2);
- //draw tick
- painter.setPen(pL);
- for(int i=0;i<60;i++)
- {
- double delt=2*M_PI/60;
- double R,r;
- R=SL/2;
- r=0.95*R;
- QPointF p1(pc.x()+R*qCos(delt*i),pc.y()+R*qSin(delt*i));
- QPointF p2(pc.x()+r*qCos(delt*i),pc.y()+r*qSin(delt*i));
- painter.drawLine(p1,p2);
- }
- painter.setPen(pW);
- for(int i=0;i<12;i++)
- {
- double delt=2*M_PI/12;
- double R,r;
- R=SL/2;
- r=0.9*R;
- QPointF p1(pc.x()+R*qCos(delt*i),pc.y()+R*qSin(delt*i));
- QPointF p2(pc.x()+r*qCos(delt*i),pc.y()+r*qSin(delt*i));
- painter.drawLine(p1,p2);
- }
- QTime now=QDateTime::currentDateTime().time();
- double angle;
- int s=now.second();
- int m=now.minute();
- int h=now.hour();
- QString str=QString("%1:%2:%3")\
- .arg(h,2,10,QChar('0'))\
- .arg(m,2,10,QChar('0'))\
- .arg(s,2,10,QChar('0'));
- setWindowTitle(str);
- painter.setFont(QFont("Arial",20,1,false));
- painter.drawText(pc.x()+30,pc.y(),str);
- //draw hour
- painter.setPen(ph);
- double Rh=SL/2*0.7;
- if(h>12)h-=12;
- angle=((h+m/60.0f+s/3600.0f)*30-90)*M_PI/180.0;
- QPointF ph(pc.x()+Rh*qCos(angle),pc.y()+Rh*qSin(angle));
- painter.drawLine(pc,ph);
- //draw minute
- painter.setPen(pm);
- double Rm=SL/2*0.8;
- angle=((m+s/60.0f)*6-90)*M_PI/180.0;
- QPointF pm(pc.x()+Rm*qCos(angle),pc.y()+Rm*qSin(angle));
- painter.drawLine(pc,pm);
- //draw second
- painter.setPen(ps);
- double Rs=SL/2*0.95;
- angle=(s*6-90)*M_PI/180.0;
- QPointF ps(pc.x()+Rs*qCos(angle),pc.y()+Rs*qSin(angle));
- painter.drawLine(pc,ps);
- }
- void Dialog::resizeEvent(QResizeEvent *event)
- {
- SL=event->size().width();
- if(SL>event->size().height())
- SL=event->size().height();
- SL*=0.95;
- }
- void Dialog::timerEvent(QTimerEvent *event)
- {
- Q_UNUSED(event)
- update();
- }
复制代码 主要的功能代码在paintEvent中,就是定时给窗口重绘的事件,然后在timerEvent中反复调用update,让窗口重绘。
将代码放入虚拟机Ubuntu中,然后还是按上节的方法,编译:
编译完成以后,放入开发板中,赋予可执行权限,运行:
最后运行效果如下(没有显示屏,只能使用MobaXTerm的X Server先显示):
|
|