使用QPainter 画饼图

具体如图所示:

使用QPainter 画饼图_第1张图片

使用QPainter 画饼图_第2张图片

当单击不同的tab标签时,根据表中的数据显示不同的饼图。

具体部分代码如下所示:

//根据数据绘制饼图
void ProTeam::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(510,420,10,10,Qt::red);
    painter.fillRect(510,440,10,10,Qt::blue);
    painter.fillRect(510,460,10,10,Qt::yellow);
    painter.fillRect(510,480,10,10,Qt::green);
    double first=ui->lineFirst->text().toDouble();
    double second=ui->lineSecond->text().toDouble();
    double third=ui->lineThird->text().toDouble();
    double fourth=ui->lineFour->text().toDouble();
    double sum=first+second+third+fourth;
    QPainter painter2(this);//四种信号量的饼状图绘制
    painter2.setBrush(Qt::red);
    painter2.setPen(Qt::NoPen);
    painter2.drawPie(400,400,100,100,0,360*16*first/sum);
    painter2.setBrush(Qt::blue);
    painter2.drawPie(400,400,100,100,360*16*first/sum,360*16*second/sum);
    painter2.setBrush(Qt::yellow);
    painter2.drawPie(400,400,100,100,360*16-360*16*third/sum-360*16*fourth/sum,360*16*third/sum);
    painter2.setBrush(Qt::green);
    painter2.drawPie(400,400,100,100,360*16-360*16*fourth/sum,360*16*fourth/sum);
}
//当lineEdit框中的内容发生改变时,所画的饼图也跟着改变
void ProTeam::on_lineFirst_textChanged(const QString &arg1)
{
    update();
}
 
  
void ProTeam::on_lineSecond_textChanged(const QString &arg1)
{
    update();
}
 
  
void ProTeam::on_lineThird_textChanged(const QString &arg1)
{
    update();
}
 
  
void ProTeam::on_lineFour_textChanged(const QString &arg1)
{
    update();
}
 当单击不同的tab标签时,传递标签下面所对应的表格的数据。 
  

void ProTeam::on_tabWidget_tabBarClicked(int index)
{
    if(index==0)
    {
       int row=ui->tableWidget->rowCount()-1;
       ui->lineFirst->setText(ui->tableWidget->item(row,3)->text());
       ui->lineSecond->setText(ui->tableWidget->item(row,4)->text());
       ui->lineThird->setText(ui->tableWidget->item(row,5)->text());
       ui->lineFour->setText(ui->tableWidget->item(row,6)->text());
       ui->label->setText(codeChange("已完成任务"));
       ui->label_2->setText(codeChange("未完成任务"));
       ui->label_3->setText(codeChange("过期任务"));
       ui->label_4->setText(codeChange("关闭任务"));
    }
    else if(index==1)
    {
        int row=ui->tableWidget_2->rowCount()-1;
        ui->lineFirst->setText(ui->tableWidget_2->item(row,2)->text());
        ui->lineSecond->setText(ui->tableWidget_2->item(row,3)->text());
        ui->lineThird->setText(ui->tableWidget_2->item(row,4)->text());
        ui->lineFour->setText(ui->tableWidget_2->item(row,5)->text());
        ui->label->setText(codeChange("已修正BUG"));
        ui->label_2->setText(codeChange("未修正BUG"));
        ui->label_3->setText(codeChange("保留BUG"));
        ui->label_4->setText(codeChange("关闭BUG"));
    }
    else if(index==2)
    {
        int row=ui->tableWidget_3->rowCount()-1;
        ui->lineFirst->setText(ui->tableWidget_3->item(row,3)->text());
        ui->lineSecond->setText(ui->tableWidget_3->item(row,4)->text());
        ui->lineThird->setText(ui->tableWidget_3->item(row,5)->text());
        ui->lineFour->setText(ui->tableWidget_3->item(row,6)->text());
        ui->label->setText(codeChange("已测试"));
        ui->label_2->setText(codeChange("未测试"));
        ui->label_3->setText(codeChange("已关闭测试"));
        ui->label_4->setText(codeChange("过期测试"));
    }
}
 
  
//编码转换
QString ProTeam::codeChange(const QByteArray &str){
    QTextCodec* codec = QTextCodec::codecForName("gb2312");
    QString s;
    s = codec->toUnicode(str);
    return s;
    }
 //使表格处于不可编辑状态代码如下:
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
   



你可能感兴趣的:(Qt)