QT Analog Clock

Source: Internet
Author: User

Briefly

One of the examples of QT comes with the clock, which demonstrates how to simplify drawing custom parts with Qpainter's conversion and scaling features.

It mainly contains the hour and minute hand drawing, does not contain the second hand. Below, we extend it on the basis of the original example.

    • Briefly
    • Implementation method
    • Example
      • Effect
      • Source
    • More references

Implementation method

Since the clock is a wonderful update, we need to refresh it regularly, and the clock will draw using the Qpainter 2D graphic we talked about earlier.

    1. With Qtimer timed refresh, set the timeout to 1000 milliseconds (1 seconds).
    2. The clock is drawn through the paintevent (), including: Hour, minute, second hand, and Panel, dial, etc.
Example effects

Source

First, we construct a timer qtimer, which connects its timeout signal to timeout () to the Slot function update (), and when update () is called, the system automatically notifies the current interface to redraw (PaintEvent ()).

MainWindow::MainWindow(QWidget *parent)    : CustomWindow(parent){    ...    // 构造定时器,设置超时为1秒    QTimer *timer = new QTimer(this);    connect(timer, SIGNAL(timeout()), this, SLOT(update()));    timer->start(1000);    ...}

Rewrite PaintEvent () to draw the clock.

void MainWindow::p aintevent (qpaintevent *event) {q_unused (event);Hour, minute, second position-polygon static const Qpoint hourhand[3] = {Qpoint (7,8), Qpoint (-7,8), Qpoint (0, - -)    };static const Qpoint minutehand[3] = {Qpoint (7,8), Qpoint (-7,8), Qpoint (0, - $)    };static const Qpoint secondhand[3] = {Qpoint (7,8), Qpoint (-7,8), Qpoint (0, - the)    };Hour, minute, seconds color Qcolor hourcolor (127,0, -);Qcolor Minutecolor (0,127,127, -);Qcolor Secondcolor (0, the, the, -);int side = Qmin (width (), height ());Qtime time = Qtime::currenttime ();qpainter painter (This);Painter. Setrenderhint(qpainter::antialiasing);Translation coordinate system origin to center point painter. Translate(Width ()/2, height ()/2);Zoom Painter. scale(Side/200.0, Side/200.0);Draw the hour painter. Setpen(Qt::nopen);Painter. Setbrush(Hourcolor);Painter. Save();Per lap the° = Ah i.e.: rotation angle = number of hours * -°painter. Rotate(30.0* (Time. Hour() + Time. Minute() /60.0)));Painter. Drawconvexpolygon(Hourhand,3);Painter. Restore();Painter. Setpen(Hourcolor);Draw the Hour line ( theDimension A= -degrees) for (int i =0; i <; ++i) {Painter. DrawLine( the,0, the,0);Painter. Rotate(30.0);}//Draw minute hand painter. Setpen(Qt::nopen);Painter. Setbrush(Minutecolor);Painter. Save();Per lap the° = -m i.e.: rotation angle = number of minutes *6°painter. Rotate(6.0* (Time. Minute() + Time. Second() /60.0));Painter. Drawconvexpolygon(Minutehand,3);Painter. Restore();Painter. Setpen(Minutecolor);Draw the minute line ( theDimension -=6degrees) for (int j =0; J <; ++j) {if ((j%5) !=0) Painter. DrawLine( the,0, the,0);Painter. Rotate(6.0);}//Draw the second hand painter. Setpen(Qt::nopen);Painter. Setbrush(Secondcolor);Painter. Save();Per lap the° = -S is: rotation angle = number of seconds *6°painter. Rotate(6.0* Time. Second());Painter. Drawconvexpolygon(Secondhand,3);Painter. Restore();}

Well, the notes are written very clearly, not much to explain, if you want to do an application, you can implement some of the settings of the pointer, the appearance of the dial style some interface.

More references
    • Analog Clock Example-Assistant

QT Analog Clock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.