Qt: Lantern code Comprehension

Source: Internet
Author: User

The walking horse lamp is a sample code commonly used in programming learning. Here we will summarize the learned knowledge. If any, please correct me.

I. Ideas

When there is no space available, enter the displayed content in a loop.

There must be a timer; a timer event to process; the displayed text content; when the displayed content moves in the control, the control needs to be re-painted.

When the control is hidden, stop the drive lamp.

Process hidden events of controls.

When the control is displayed, turn the horse lamp.

Handles display events of controls.

Ii. header files

# Ifndef mainwidget_h # define mainwidget_h # include <qwidget> extends qtimerevent; Class qshowevent; Class qhideevent; Class qpaintevent; qt_end_namespacenamespace UI {class mainwidget;} class mainwidget: public qwidget {q_object // attribute q_property (qstring text read text write settext) Public: explicit mainwidget (qwidget * parent = 0 );~ Mainwidget (); // receive the text void settext (const qstring & newtext); // return and save the text qstring text () const; // return the most suitable control size. Virtual qsize sizehint () const; // redraw the control virtual void paintevent (qpaintevent * E); protected: // process the timer event virtual void timerevent (qtimerevent * E); // process the display event virtual void showevent (qshowevent * E) of the control ); // process the control hidden event virtual void hideevent (qhideevent * E); Private: Ui: mainwidget * UI; // Save the text variable qstring mytext; // Save the display text offset int offset; // Save the timer ID int mytimerid;}; # endif/mainwidget_h

The following describes how to implement and annotate a method.

When the control is displayed, the timer is called every 30 minutes. Assign the timer ID to mytimerid.

void MainWidget::showEvent(QShowEvent *e){    myTimerId = startTimer(30);}

When the timer is activated, the timer of the control is called to process the event.

When a timer event is triggered, first determine whether the timer that triggers this event is a trojan timer. If yes, the offset variable is automatically added with 1, and the content on the control panel is shifted to 1 pixel. This is equivalent to the coordinate (0, 0) in the upper left corner of the control panel, but the leftmost coordinate of the Panel is ). When you re-paint a panel in a paintevent event, the content is always drawn from the leftmost end. In this way, the content is constantly moving to the left. Form a running horse lamp effect. In other cases, the timer event of the parent class is called for processing.

Void mainwidget: timerevent (qtimerevent * E) {If (e-> timerid () = mytimerid) {++ offset; /** If yes, the offset variable is automatically incremented by 1, and the content on the control panel is shifted by 1 pixel. This is equivalent to the coordinate of the upper left corner of the Control Panel (0, 0), but the leftmost coordinate of the Panel is ). When you re-paint a panel in a paintevent event, the content is always drawn from the leftmost end. In this way, the content is constantly moving to the left. Form a running horse lamp effect. */If (Offset> = fontmetrics (). width (text () {offset = 0;} scroll (-1, 0);} else {// call the parent class timer event for processing. Qwidget: timerevent (e );}}

When the control is hidden, kill the timer. Set the timer ID variable to 0.

void MainWidget::hideEvent(QHideEvent *e){    Q_UNUSED(e);    killTimer(myTimerId);    myTimerId = 0;}

When the control changes (the control moves, the size changes, etc.), the control panel is re-painted.

void MainWidget::paintEvent(QPaintEvent *e){    Q_UNUSED(e);    QPainter painter(this);    //set the text width and height.    const int textWidth(fontMetrics().width(text()));    const int textHeight(fontMetrics().height());    //no text show.    if (1 > textWidth) {        return;    }    //set the text begin in (-offset, 0).    int x(-offset);    while (width() > x) {        painter.drawText(x,                         0,                         textWidth,                         textHeight,                         Qt::AlignLeft | Qt::AlignVCenter,                         text());        //        x += textWidth;    }}


Qt: Lantern code Comprehension

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.