//此程序功能要画出一个统计图中的饼形图
我有一段画图的程序:
void countPage::paintDispalyLabel()
{
//清空QLabel
backPicture->fill();
//准备颜色
vector<QString> colorPlatte;
colorPlatte.push_back(QString("Qt::red"));
colorPlatte.push_back(QString("Qt::blue"));
colorPlatte.push_back(QString("Qt::green"));
colorPlatte.push_back(QString("Qt::cyan"));
colorPlatte.push_back(QString("Qt::magenta"));
colorPlatte.push_back(QString("Qt::yellow"));
colorPlatte.push_back(QString("Qt::gray"));
colorPlatte.push_back(QString("Qt::black"));
colorPlatte.push_back(QString("Qt::darkred"));
colorPlatte.push_back(QString("Qt::darkblue"));
colorPlatte.push_back(QString("Qt::darkgreen"));
colorPlatte.push_back(QString("Qt::darkcyan"));
colorPlatte.push_back(QString("Qt::darkmagenta"));
vector<double> saveTotal;
double temp;
double realTotal=0; //总数
for(int i=0;i<caker;i++)
{
//记录单项的数目。取绝对值,并累加总数
temp=(displayTable->text(i,4)).toDouble();
if(temp<0)temp=-temp;
saveTotal.push_back(temp);
realTotal+=temp;
}
int currentDegree=0; //画的饼形的初始角度
int increaseDegree=0; //画的饼形的角度
QPainter paint(backPicture);
for(int i=0;i<caker;i++)
{
paint.setPen(Qt::black);
paint.setBrush(QColor(colorPlatte[i%13]));
increaseDegree=(int)((saveTotal/realTotal)*5760); //计算单次画的角度
paint.drawPie((WIDTH-2*SEMI)/2,(WIDTH-2*SEMI)/2,2*SEMI,2*SEMI,currentDegree,increaseDegree);
cout<<"tell me what i is"<<i<<endl;
cout<<"tell me what caker is"<<caker<<endl;
cout<<"tell me what colorname is"<<colorPlatte[i%13]<<endl;
cout<<"tell me what increaseDegree is"<<increaseDegree<<endl;
currentDegree+=increaseDegree; //计算下一次的初始角度
}
displayLabel->setPixmap(*backPicture);
}
其中displayLabel是一个QLabel,backPicture是一个QPixmap。displayTable是一个QTable。
这段程序编译成功,能够运行,终端输出为:
tell me what i is0
tell me what caker is3
tell me what colorname isQt::red
tell me what increaseDegree is680
tell me what i is1
tell me what caker is3
tell me what colorname isQt::blue
tell me what increaseDegree is327
tell me what i is2
tell me what caker is3
tell me what colorname isQt::green
tell me what increaseDegree is4752
能够画出来一个圆形,但是整个园都是黑颜色,令我很不解。。。。这是为什么呢?