QTableWidget逆序插入(高性能插入)

首先我们看下QTableWidget正序创建Item

    ui->charttable->setColumnCount(10);
    ui->charttable->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->charttable->setSelectionMode(QTableWidget::SingleSelection);
    ui->charttable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->charttable->setFocusPolicy(Qt::NoFocus);
    ui->charttable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->charttable->setHorizontalHeaderLabels(QStringList()<<"编号"<<"名字"<<"年龄";
    ui->charttable->setRowCount(10);
    for (int i = 0; i < 10 i++)
    {
        QTableWidgetItem* item1 = new QTableWidgetItem(QString::number(i));
        QTableWidgetItem* item2 = new QTableWidgetItem("name:"+QString::number(i));
        QTableWidgetItem* item3 = new QTableWidgetItem("age:"+QString::number(i));
        item1->setTextAlignment(Qt::AlignCenter);
        item2->setTextAlignment(Qt::AlignCenter);
        item3->setTextAlignment(Qt::AlignCenter);
        ui->charttable->setItem(i,0,item1);
        ui->charttable->setItem(i,1,item2);
        ui->charttable->setItem(i,2,item3);
    }
    ui->charttable->setShowGrid(false);

正序插入2行:

        ui->charttable->setRowCount(ui->charttable->rowCount()+2);
        for (int i = ui->charttable->rowCount(); i < ui->charttable->rowCount()+2; i++)
        {
            QTableWidgetItem* item1 = new QTableWidgetItem(QString::number(i));
           QTableWidgetItem* item2 = new QTableWidgetItem("name:"+QString::number(i));
            QTableWidgetItem* item3 = new QTableWidgetItem("age:"+QString::number(i));
            item1->setTextAlignment(Qt::AlignCenter);
            item2->setTextAlignment(Qt::AlignCenter);
            item3->setTextAlignment(Qt::AlignCenter);
            ui->charttable->setItem(0,0,item1);
            ui->charttable->setItem(0,1,item2);
            ui->charttable->setItem(0,2,item3);
        }

逆序插入2行:

        for (int i = ui->charttable->rowCount(); i < ui->charttable->rowCount()+2; i++)
        {
            ui->charttable->insertRow(0); //从第一行插入
            QTableWidgetItem* item1 = new QTableWidgetItem(QString::number(0));
            QTableWidgetItem* item2 = new QTableWidgetItem("name:"+QString::number(i));
            QTableWidgetItem* item3 = new QTableWidgetItem("age:"+QString::number(i));
            item1->setTextAlignment(Qt::AlignCenter);
            item2->setTextAlignment(Qt::AlignCenter);
            item3->setTextAlignment(Qt::AlignCenter);
            ui->charttable->setItem(0,0,item1);
            ui->charttable->setItem(0,1,item2);
            ui->charttable->setItem(0,2,item3);
        }
        //更新编号
        for (int i = 0; i < ui->charttable->rowCount(); i++)
        {
            ui->charttable->item(i,0)->setText(QString::number(i));
        }

逆序插入删除选中行。

        ui->charttable->removeRow(ui->charttable->currentRow());
        //更新编号
        for (int i = 0; i < ui->charttable->rowCount(); i++)
        {
            ui->charttable->item(i,0)->setText(QString::number(i));
        }

通过以上的例子,是不是发现了正序和逆序它们操作方式不一样。正序是通过setRowCount来增加行的,逆序是通过insertRow来增加行的。但是逆序还需要更新编号和反向取数据。

当然我们正序的时候也可以用insertRow来实现,所以它跟setRowCount有什么区别,经过实际测试,我发现当数据量较大时,insertRow比setRowCount快很多,当然想想它的源码逻辑也能猜测到insertRow要快很多,因为setRowCount它需要先遍历操作找到最后一条插入行,然后新增。而insertRow在指定位置新增,更新行数就可以了。

你可能感兴趣的:(QT,qt)