欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

QT 绘制表格以及内容的居中显示

程序员文章站 2022-06-28 15:21:13
项目中第一次用到表格的绘制的功能,实现表格的行列的绘制以及表格中字符串的居中显示,先看下显示的效果! #include#includeDistanceAnalysisDialog.cppvoid DistanceAnalysisDialog::drawTable(void){ //存储的累计次数 int QX_numZeroToPointTwo=0; int......

项目中第一次用到表格的绘制的功能,实现表格的行列的绘制以及表格中字符串的居中显示,先看下显示的效果!

          QT   绘制表格以及内容的居中显示

#include<QStandardItemModel>
#include<QTableView>

DistanceAnalysisDialog.cpp

void DistanceAnalysisDialog::drawTable(void)
{
    //存储的累计次数
    int QX_numZeroToPointTwo=0;
    int QX_numPointTwoToPointFive=0;
    int QX_numPointFiveToPointEight=0;
    int QX_numPointEightToOne=0;
    int QX_greaterThanOne=0;
    int LF_numZeroToPointTwo=0;
    int LF_numPointTwoToPointFive=0;
    int LF_numPointFiveToPointEight=0;
    int LF_numPointEightToOne=0;
    int LF_greaterThanOne=0;
    int QX_salution=0;
    int LF_salution=0;
    //存储的是百分之占比
    double QX_numZeroToPointTwoNum=0.0;
    double QX_numPointTwoToPointFiveNum=0.0;
    double QX_numPointFiveToPointEightNum=0.0;
    double QX_numPointEightToOneNum=0.0;
    double QX_greaterThanOneNum=0.0;
    double LF_numZeroToPointTwoNum=0.0;
    double LF_numPointTwoToPointFiveNum=0.0;
    double LF_numPointFiveToPointEightNum=0.0;
    double LF_numPointEightToOneNum=0.0;
    double LF_greaterThanOneNum=0.0;
    double QX_numSalution=0.0;
    double LF_numSalution=0.0;
   //表格头的创建及显示内容输入
    QStandardItemModel *model = new QStandardItemModel(2,7);
    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("基站名称"));
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("0~0.2"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("0.2~0.5"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("0.5~0.8"));
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("0.8~1"));
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral(">1"));
    model->setHeaderData(6,Qt::Horizontal,QStringLiteral("固定解占比"));
    for (int i=0;i<ENU->horizontalErrorListOne.length();i++)
    {
        double num= ENU->horizontalErrorListOne.at(i);
        if(0<num&&num<=0.2)
        {
            QX_numZeroToPointTwo++;
        }
        else if(0.2<num&&num<=0.5)
        {
            QX_numPointTwoToPointFive++;
        }
        else if(0.5<num&&num<=0.8)
        {
            QX_numPointFiveToPointEight++;
        }
        else if(0.8<num&&num<=1)
        {
            QX_numPointEightToOne++;
        }
        else if(num>1)
        {
            QX_greaterThanOne++;
        }
    }
    for (int i=0;i<ENU->horizontalErrorListTwo.length();i++)
    {
        double num= ENU->horizontalErrorListTwo.at(i);
        if(0<num&&num<=0.2)
        {
            LF_numZeroToPointTwo++;
        }
        else if(0.2<num&&num<=0.5)
        {
            LF_numPointTwoToPointFive++;
        }
        else if(0.5<num&&num<=0.8)
        {
            LF_numPointFiveToPointEight++;
        }
        else if(0.8<num&&num<1)
        {
            LF_numPointEightToOne++;
        }
        else if(1<num)
        {
            LF_greaterThanOne++;
        }
    }
    for(int i=0;i<fileOperation->sceneFileList1->length();i++)
    {
        QString salution= fileOperation->sceneFileList1->at(i).solutionState;
        if(salution=="4")
        {
            QX_salution++;
        }
    }
    for(int i=0;i<fileOperation->sceneFileList->length();i++)
    {
        QString salution= fileOperation->sceneFileList->at(i).solutionState;
        if(salution=="4")
        {
            LF_salution++;
        }
    }
    QX_numZeroToPointTwoNum= (static_cast<double>(QX_numZeroToPointTwo)/(ENU->horizontalErrorListOne.length()))*100;
    QX_numPointTwoToPointFiveNum=(static_cast<double>(QX_numPointTwoToPointFive)/ENU->horizontalErrorListOne.length())*100;
    QX_numPointFiveToPointEightNum= (static_cast<double>(QX_numPointFiveToPointEight)/ENU->horizontalErrorListOne.length())*100;
    QX_numPointEightToOneNum=(static_cast<double>(QX_numPointEightToOne)/ENU->horizontalErrorListOne.length())*100;
    QX_greaterThanOneNum= (static_cast<double>(QX_greaterThanOne)/ENU->horizontalErrorListOne.length())*100;
    LF_numZeroToPointTwoNum=(static_cast<double>(LF_numZeroToPointTwo)/ENU->horizontalErrorListTwo.length())*100;
    LF_numPointTwoToPointFiveNum=(static_cast<double>(LF_numPointTwoToPointFive)/ENU->horizontalErrorListTwo.length())*100;
    LF_numPointFiveToPointEightNum=(static_cast<double>(LF_numPointFiveToPointEight)/ENU->horizontalErrorListTwo.length())*100;
    LF_numPointEightToOneNum=(static_cast<double>(LF_numPointEightToOne)/ENU->horizontalErrorListTwo.length())*100;
    LF_greaterThanOneNum=(static_cast<double>(LF_greaterThanOne)/ENU->horizontalErrorListTwo.length())*100;
    //解状态占比
    QX_numSalution=(static_cast<double>(QX_salution)/(fileOperation->sceneFileList1->length()))*100;
    LF_numSalution=(static_cast<double>(LF_salution)/(fileOperation->sceneFileList->length()))*100;
    //表格中输入数据
    model->setData(model->index(0,0), QStringLiteral("千寻位置"));
    model->setData(model->index(0,1), QString::number(QX_numZeroToPointTwoNum,'f',2));
    model->setData(model->index(0,2), QString::number(QX_numPointTwoToPointFiveNum,'f',2));
    model->setData(model->index(0,3), QString::number(QX_numPointFiveToPointEightNum,'f',2));
    model->setData(model->index(0,4), QString::number(QX_numPointEightToOneNum,'f',2));
    model->setData(model->index(0,5), QString::number(QX_greaterThanOneNum,'f',2));
    model->setData(model->index(0,6), QString::number(QX_numSalution,'f',2));
    model->setData(model->index(1,0), QStringLiteral("六分科技"));
    model->setData(model->index(1,1), QString::number(LF_numZeroToPointTwoNum,'f',2));
    model->setData(model->index(1,2), QString::number(LF_numPointTwoToPointFiveNum,'f',2));
    model->setData(model->index(1,3), QString::number(LF_numPointFiveToPointEightNum,'f',2));
    model->setData(model->index(1,4), QString::number(LF_numPointEightToOneNum,'f',2));
    model->setData(model->index(1,5), QString::number(LF_greaterThanOneNum,'f',2));
    model->setData(model->index(1,6), QString::number(LF_numSalution,'f',2));
    //实现表格内容的居中显示
    for(int i=0;i<2;i++)
    {
        for (int j=0;j<7;j++)
        {
             model->item(i,j)->setTextAlignment(Qt::AlignCenter);
        }
    }
ui->tableView->resizeColumnsToContents(); //根据内容调整大小
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据容器调整大小
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//只读
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选中模式为选中行
ui->tableView->setSelectionMode( QAbstractItemView::SingleSelection); //设置选中单行
ui->tableView->setModel(model); 
}

 

本文地址:https://blog.csdn.net/qq_33263769/article/details/108572769

相关标签: QT