10 ticker of QT Study Notes

Source: Internet
Author: User

Bored learning ......

Write a ticker to trigger an event in the exercise.

========================================================== ==================================

Some code is as follows:

Ticker. h

# Ifndef ticker_h <br/> # define ticker_h <br/> # include <qtgui/qwidget> <br/> namespace UI <br/>{< br/> class ticker; <br/>}< br/> class ticker: Public qwidget <br/>{< br/> q_object <br/> public: <br/> ticker (qwidget * parent = 0); <br/> ~ Ticker (); <br/> void settext (const qstring & newtext); <br/> qstring text () const; <br/> qsize sizehint () const; <br/> protected: <br/> void paintevent (qpaintevent * event); <br/> void timerevent (qtimerevent * evnet ); <br/> void showevent (qshowevent * event); <br/> void hideevent (qhideevent * event); <br/> PRIVATE: <br/> UI :: ticker * UI; <br/> qstring tickertext; <br/> int offset; // time interval <br/> int timerid; // timer id <br/> }; <br/> # endif // ticker_h


Ticker. cpp

# Include <qtgui> <br/> # include "ticker. H "<br/> # include" ui_ticker.h "<br/> ticker: ticker (qwidget * parent) <br/>: qwidget (parent), UI (new UI :: ticker) <br/>{< br/> UI-> setupui (this); <br/> offset = 0; <br/> timerid = 0; <br/>}< br/> ticker ::~ Ticker () <br/>{< br/> Delete UI; <br/>}< br/>/** <br/> set text <br/> */<br/> void ticker: settext (const qstring & newtext) <br/>{< br/> tickertext = newtext; <br/> Update (); <br/> updategeometry (); <br/>}< br/>/** <br/> obtain text content <br/> */<br/> qstring ticker: Text () const <br/>{< br/> return tickertext; <br/>}< br/>/** <br/> set the widget size <br/> */<br/> qsize ticker: sizehint () const <br/>{< br/> return fontmetrics (). size (0, text (); // fontmetrics (): returns the font metrics for the widget's current font <br/>}< br/>/** <br/> draw events <br/> */<br/> void ticker:: paintevent (qpaintevent *) <br/>{< br/> qpainter painter (this); <br/> int textwidth = fontmetrics (). width (text (); <br/> If (textwidth <1) <br/> return; <br/> int x =-offset; <br/> while (x <width () {<br/> painter. drawtext (x, 0, textwidth, height (), QT: alignleft | QT: alignvcenter, text (); // draw text <br/> X + = textwidth; <br/>}< br/>/** <br/> timer event <br/> */<br/> void ticker :: timerevent (qtimerevent * event) <br/>{< br/> If (Event-> timerid () = timerid) {<br/> ++ offset; <br/> If (Offset> = fontmetrics (). width (text () <br/> offset = 0; <br/> scroll (-1, 0 ); // scroll one pixel to the left <br/>} else {<br/> qwidget: timerevent (event ); <br/>}< br/>/** <br/> show events <br/> */<br/> void ticker :: showevent (qshowevent *) <br/>{< br/> timerid = starttimer (30); // starts a timer and returns a timer identifier, or returns zero if it cocould not start a timer. <br/>}< br/>/** <br/> hide events <br/> */<br/> void ticker: hideevent (qhideevent *) <br/>{< br/> killtimer (timerid); <br/> timerid = 0; <br/>}< br/>


The interface is as follows:


You can automatically scroll to the left... (How do you feel like the electricity bill is shown downstairs)


========================================================== ============================

Continue the boring summer vacation ....


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.