📄 pie.cpp
字号:
#include <qpainter.h>#include <math.h>#include "pie.h"//// Construct the PieView with buttons.//PieView::PieView( int *n, const char **s ){ setCaption( "Pie in the Sky Inc." ); nums = n; strs = s; resize( 500, 350 );}void PieView::restart( int *n, const char **s ){ nums = n; strs = s; repaint();}//// This function draws a pie chart// void PieView::drawPies( QPainter *p ) // zero terminated{ int sum = 0; int n = 0; { int *ip = nums; while (*ip) { sum += *ip++; n++; } if ( !sum ) return; } QFont f( "times", 18, QFont::Bold ); p->setFont( f ); p->setPen( black ); // black pen outline int size; int apos = -90*16; int *val = nums; const int w = width(); const int h = height(); const int xd = w - w/5; const int yd = h - h/5; const char ** s = strs; QBrush back(white); QRect qr(0,0,70,25); int i = 0; while (( size = *val++ )) { QColor c; c.setHsv( ( i++ * 255)/n, 255, 255 ); // rainbow effect p->setBrush( c ); // solid fill with color c int a = ( size * 360 * 16 )/sum; p->drawPie( w/10, h/10, xd, yd, -apos, -a ); apos += a; int x = (int) (cos(M_PI*2* (double)(apos-a/2) / 5760.0) * xd/2); int y = (int) (sin(M_PI*2* (double)(apos-a/2) / 5760.0) * yd/2); qr.moveCenter( QPoint(x+w/2,y+h/2) ); p->setBrush(back); if ( s ) { if ( *s && **s ) { p->drawRect(qr); p->drawText(qr, AlignCenter, *s); } s++; } }}//// Called when the widget needs to be updated.//void PieView::paintEvent( QPaintEvent * ){ QPainter paint; paint.begin( this ); drawPies( &paint ); paint.end(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -