• 5263阅读
  • 5回复

画出来的图居然都是黑的! [复制链接]

上一主题 下一主题
离线shifan
 
只看楼主 倒序阅读 楼主  发表于: 2009-08-25
//此程序功能要画出一个统计图中的饼形图

我有一段画图的程序:
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

能够画出来一个圆形,但是整个园都是黑颜色,令我很不解。。。。这是为什么呢?
离线zhangyujin78
只看该作者 1楼 发表于: 2009-08-25
这个好深奥。。。。
离线shifan
只看该作者 2楼 发表于: 2009-08-25
我在Pen和Brush上试了多种组合,发现只要我设置Brush,就是一个大黑饼。
其实有时在黑饼的最外一条线上(只有一个像素)是可以看到我设置的颜色的。
如果只用Pen,线可以花出来,但是都是黑线,而且中空。
添加flush也无效。。。。怎么办啊?
离线dbzhang800

只看该作者 3楼 发表于: 2009-08-25
楼主最好弄个最简的能说明问题的例子作为附件贴上来吧。

这样让大家看,恐怕大家也都没心情
离线shifan
只看该作者 4楼 发表于: 2009-08-25
终于被我试出来了,呵呵。
colorPlatte.push_back(QString("Qt::red"));  
这种句子有问题,把Qt::删掉就可以了。QColor(QString(Qt::red));
中名字Qt::red无效,所以返回了空值,就变成黑的了。
离线dbzhang800

只看该作者 5楼 发表于: 2009-08-25
vector<QString> colorPlatte;

QColor(colorPlatte[i%13])

这地方出问题!!

好好看看这样的会语句是你想要的么?
QColor(QString("Qt::red"))
快速回复
限100 字节
 
上一个 下一个