QT: "Download speed histogram" simulation implementation--thinking really good, can use their brains, even I have inspired (this idea seems to be universal)

Source: Internet
Author: User
Tags set background

I do not know which version of the Thunderbolt, there is a "download speed histogram" of the small interface, I prefer (but the latest version of the Thunderbolt is not), so decided to cottage one. Of course, this cottage can not download files, hehe.

Ideas:
1: Paint the background of the interface black
2: Generate a random number every 0.1 seconds and add them to a container

3: Overload the PaintEvent function, starting from the right side of the interface and sequentially drawing the elements in the container in reverse order (each data is a bar)

Code:

[CPP]View Plaincopyprint?
    1. #include <QtGui>
    2. #include <QtCore>
    3. Class Barchart: Public qwidget
    4. {
    5. Q_object
    6. Private
    7. qlist<int> m_list; //Store the points recorded in the history
    8. Qsize m_size; //size of the current drawing window
    9. Qtimer M_timer; //timer, sends a signal every 0.1 seconds, simulates receiving data
    10. Protected
    11. void PaintEvent (Qpaintevent *event);
    12. void Resizeevent (Qresizeevent *event);
    13. Public
    14. Barchart (Qwidget *parent = 0);
    15. ~barchart () {}
    16. Public Slots:
    17. void Adddataslot ();
    18. };
    19. Barchart::barchart (Qwidget *parent)
    20. : Qwidget (parent)
    21. {
    22. //define Timers
    23. Qsrand (Qdatetime::currentdatetime (). Tomsecssinceepoch ());
    24. Connect (&m_timer, SIGNAL (timeout ()), this , SLOT (Adddataslot ()));
    25. M_timer.start (100);
    26. }
    27. void Barchart::p aintevent (qpaintevent *event)
    28. {
    29. const int WIDTH = 2;
    30. qpainter painter (this);
    31. //Set Background to black
    32. Painter.setbrush (Qt::black);
    33. Painter.drawrect ( -2,-2, M_size.width () +4, M_size.height () +4);
    34. Painter.setpen (Qpen (Qt::green, WIDTH));
    35. int TX, CX, CY1, Cy2;
    36. tx = 0;
    37. Cy1 = M_size.height ();
    38. //Draw a vertical line of each section
    39. qlist<Int>::iterator iter = M_list.end ();
    40. While (iter! = M_list.begin ())
    41. {
    42. Cy2 = Cy1-(* (--iter) *m_size.height ()/1000);
    43. CX = M_size.width ()-TX;
    44. Painter.drawline (CX, Cy1, CX, cy2);
    45. TX + = WIDTH;
    46. }
    47. }
    48. void Barchart::resizeevent (Qresizeevent *event)
    49. {
    50. M_size = Event->size ();
    51. Update ();
    52. }
    53. void Barchart::adddataslot ()
    54. {
    55. //Add a 0-999 of data
    56. int temp = Qrand ()% 1000;
    57. M_list.push_back (temp);
    58. //If the data is too long, throw away the previous part
    59. if (m_list.size () > M_size.width ())
    60. M_list.pop_front ();
    61. Update ();
    62. }
    63. #include "Main.moc"
    64. int main (int argc, char **argv)
    65. {
    66. Qapplication app (argc, argv);
    67. Barchart *bar = new Barchart;
    68. Bar->show ();
    69. return app.exec ();
    70. }

http://blog.csdn.net/small_qch/article/details/7585758

QT: "Download speed histogram" simulation implementation--thinking really good, can use their brains, even I have inspired (this idea seems to be universal)

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.