zoukankan      html  css  js  c++  java
  • QT:“下载速度柱状图”的模拟实现——思路真好,会动脑筋,连我都有了启发(这个思路好像是通用的)

        不知是哪个版本的迅雷,有个“下载速度柱状图”的小界面,我比较喜欢(只不过最新版本的迅雷却没了),所以决定来山寨一个。当然,这个山寨品不能下载文件,呵呵。

    思路:
    1:将界面的背景涂成黑色
    2:每隔0.1秒就产生一个随机数,将它们添加到一个容器中

    3:重载paintEvent函数,,从界面的右边开始,依次将容器中的元素按倒序画出来(每个数据就是一个柱形)

    截图:

    代码:

      1. #include <QtGui>  
      2. #include <QtCore>  
      3.   
      4. class BarChart : public QWidget    
      5. {    
      6.     Q_OBJECT    
      7. private:  
      8.     QList<int> m_List;        //储存历史上所记录的那些点  
      9.     QSize m_Size;           //当前绘图窗口的大小  
      10.     QTimer m_Timer;         //定时器,每0.1秒发出一次信号,模拟收到数据  
      11. protected:  
      12.     void paintEvent(QPaintEvent *event);  
      13.     void resizeEvent(QResizeEvent *event);  
      14. public:  
      15.     BarChart(QWidget *parent = 0);  
      16.     ~BarChart() { }  
      17.     public slots:  
      18.         void AddDataSlot();  
      19. };  
      20.   
      21. BarChart::BarChart(QWidget *parent)   
      22. : QWidget(parent)  
      23. {  
      24.     //定义定时器  
      25.     qsrand( QDateTime::currentDateTime().toMSecsSinceEpoch() );  
      26.     connect(&m_Timer, SIGNAL(timeout()), this, SLOT(AddDataSlot()));  
      27.     m_Timer.start(100);  
      28. }  
      29.   
      30. void BarChart::paintEvent(QPaintEvent *event)  
      31. {  
      32.     const int WIDTH = 2;  
      33.     QPainter painter(this);  
      34.     //设置背景为黑色  
      35.     painter.setBrush(Qt::black);  
      36.     painter.drawRect(-2, -2, m_Size.width()+4, m_Size.height()+4);  
      37.   
      38.     painter.setPen( QPen(Qt::green, WIDTH) );  
      39.     int tx, cx, cy1, cy2;  
      40.     tx = 0;  
      41.     cy1 = m_Size.height();  
      42.   
      43.     //画出各段竖线  
      44.     QList<int>::iterator iter = m_List.end();  
      45.     while( iter != m_List.begin() )  
      46.     {  
      47.         cy2 = cy1 - (*(--iter)*m_Size.height()/1000);  
      48.         cx = m_Size.width() - tx;  
      49.         painter.drawLine(cx, cy1, cx, cy2);  
      50.         tx += WIDTH;  
      51.     }  
      52.   
      53. }  
      54.   
      55. void BarChart::resizeEvent(QResizeEvent *event)  
      56. {  
      57.     m_Size = event->size();  
      58.     update();  
      59. }  
      60.   
      61. void BarChart::AddDataSlot()  
      62. {  
      63.     //添加一个0-999的数据  
      64.     int temp = qrand() % 1000;  
      65.     m_List.push_back(temp);  
      66.     //如果数据太长了,就丢掉前面的那一部分  
      67.     if( m_List.size() > m_Size.width() )  
      68.         m_List.pop_front();  
      69.     update();  
      70. }  
      71.   
      72. #include "main.moc"  
      73.   
      74. int main(int argc, char **argv)  
      75. {  
      76.     QApplication app(argc, argv);  
      77.     BarChart *bar = new BarChart;  
      78.     bar->show();  
      79.     return app.exec();  
      80. }  

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

  • 相关阅读:
    体验cygwin纪实
    播布客视频PIT专用播放器MBOO2015
    rpm基本命令参考
    rhel7.x配置本地yum
    mtr网络连通性测试
    Oracle下载汇聚
    Spring Cloud心跳监测
    Hystrix的用法
    Redis系列十:缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级
    dubbo异步调用三种方式
  • 原文地址:https://www.cnblogs.com/findumars/p/4993545.html
Copyright © 2011-2022 走看看