In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Qt draws the clock interface in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, let's take a look at the effect:
It should be noted that here you need to create an interface with a base class of Widget to draw the clock, so that later if you want to put the clock into any interface, you just need to add a widget to the ui of that interface, and then upgrade it to the clock interface and set its size.
Widget.h file
# ifndef WIDGET_H#define WIDGET_H# include#include#include#include#include#include # include namespace Ui {class Widget;} class Widget: public QWidget {Q_OBJECT public: explicit Widget (QWidget * parent = 0); ~ Widget (); protected: void paintEvent (QPaintEvent * event); private: Ui::Widget * ui;}; # endif / / WIDGET_H
Widget.cpp file
# include "widget.h" # include "ui_widget.h" Widget::Widget (QWidget * parent): QWidget (parent), ui (new Ui::Widget) {ui- > setupUi (this); QTimer * timer = new QTimer (this); / / Connect signal and slot connect (timer, SIGNAL (timeout ()), this, SLOT (update (); timer- > start (1000); / / set form name and size setWindowTitle (tr ("Clock")) Ui- > setupUi (this); resize (350350);} Widget::~Widget () {delete ui;} void Widget::paintEvent (QPaintEvent * event) {static const QPoint hourHand [3] = {QPoint (5,14), QPoint (- 5,14), QPoint (0,-50)} Static const QPoint minuteHand [3] = {QPoint (5Power14), QPoint (- 5recover14), QPoint (0,-70)}; static const QPoint secondHand [3] = {QPoint (5Power14), QPoint (- 5Power14), QPoint (0,-80)} Static const QPoint line_long [2] = {QPoint (0100), QPoint (0jue 80)}; static const QPoint line_short [2] = {QPoint (0100), QPoint (0Power95)}; / / the drawn range int side = qMin (width (), height ()); / / get the current time QTime time = QTime::currentTime () / declare the painter QPainter painter (this) for drawing; / / Anti-aliasing painter.setRenderHint (QPainter::Antialiasing) for the drawn image; / / relocate the coordinate starting point and place the coordinate origin to the central painter.translate of the form (width () / 2, height () / 2) / / add words to the dial (arbitrarily) / / painter.drawText (- 75 fuzz 120min 10055050) 170pr ("ROLEX"); / / set the border of the canvas to painter.scale (side / 200.0, side / 200.0); / / set the fill edge to black, or set it to NoPen painter.setPen (Qt::black); / / set the brush color to painter.setBrush (Qt::black) / / Save the state of the painter painter.save (); / / set the rotation angle of the painter painter.rotate (30.0 * ((time.hour () + time.minute () / 60.0)); / / the clockwise region painter.drawConvexPolygon (hourHand, 3); painter.restore (); painter.setPen (Qt::black); / / 12 scale cycles for (int I = 0; I < 12) + + I) {painter.drawLine (line_long [0], line_long [1]); painter.rotate (30.0);} / / draw minute hand corner and scale painter.setPen (Qt::black); painter.setBrush (Qt::black); painter.save (); painter.rotate (6.0 * (time.minute () + time.second () / 60.0); painter.drawConvexPolygon (minuteHand, 3) Painter.restore (); painter.setPen (Qt::black); for (int j = 0; j < 60; + + j) {if ((j% 5)! = 0) painter.drawLine (line_short [0], line_short [1]); painter.rotate (6.0);} / / draw minute hand rotation, scale painter.setPen (Qt::black); painter.setBrush (Qt::black) Painter.save (); painter.rotate (6.0* (time.minute () + time.second () / 60.0); painter.drawConvexPolygon (minuteHand, 3); painter.restore (); painter.setPen (Qt::black); for (int j = 0; j < 60; + + j) {if ((j% 5)! = 0) painter.drawLine (line_short [0], line_short [1]) Painter.rotate;} / / draw second hand corner painter.setPen (Qt::black); painter.setBrush (Qt::black); painter.save (); painter.rotate (6.0*time.second ()); painter.drawConvexPolygon (secondHand,3); painter.restore ();}
Main.cpp file
# include "widget.h" # include int main (int argc, char * argv []) {QApplication a (argc, argv); Widget w; w.show (); return a.exec ();} Thank you for reading! This is the end of the article on "how to draw the clock interface in Qt in C++". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.