QTableWidgetItem 实现拖拽复制,覆盖 上下移动 删除

QTableWidgetItem

拖拽 复制 覆盖 :

这里写图片描述

    //The view supports both dragging and dropping
    ui->tableWidget->setDragDropMode(QAbstractItemView::DragDrop);

    ui->tableWidget->setDragEnabled(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//选中一行

移动一行函数:

这里写图片描述

void Widget::moveRow(QTableWidget *pTable, int currentRow, int toRow)
{
    if(pTable == NULL)
        return;

    int nRowCount = pTable->rowCount();
    int nColumnCount = pTable->columnCount();

    pTable->setFocus();

    if(currentRow == toRow)
        return;

    if(currentRow < 0 || toRow <0 ||currentRow > nRowCount || toRow > nRowCount)
        return;

    if(toRow < currentRow)
        currentRow++;

    pTable->insertRow(toRow);

    for(int i = 0; i <nColumnCount;i++)
    {
        pTable->setItem(toRow,i,pTable->takeItem(currentRow,i));
    }
    if(currentRow < toRow)
        toRow--;

    pTable->removeRow(currentRow);
    pTable->selectRow(toRow);

}
//调用
void Widget::on_moveUp_clicked()
{
    moveRow(ui->tableWidget,ui->tableWidget->currentRow(),ui->tableWidget->currentRow() - 1);
}

void Widget::on_moveDown_clicked()
{
    moveRow(ui->tableWidget,ui->tableWidget->currentRow(),ui->tableWidget->currentRow() + 2);
}

删除一行:

这里写图片描述

void Widget::on_remove_clicked()
{
    ui->tableWidget->removeRow(ui->tableWidget->currentRow());
}

代码:

//界面初始化
void Widget::initUI()
{
    this->setWindowTitle("码农小明-QTabWidgetItem 拖拽 移动 删除");

    QStringList headLabelList;

    headLabelList << tr("Name") << tr("Speed") << tr("Delay");

    ui->tableWidget->setColumnCount(headLabelList.length());

    ui->tableWidget->setHorizontalHeaderLabels(headLabelList);

    for(int i = 0; i < 3 ;i++)
    {
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());//插入一行

        int row = ui->tableWidget->rowCount() - 1;

        ui->tableWidget->setItem(row,0,new QTableWidgetItem(QString("%1").arg(i+1)) );
        ui->tableWidget->setItem(row,1,new QTableWidgetItem(QString("%1").arg(i+1*100)) );
        ui->tableWidget->setItem(row,2,new QTableWidgetItem(QString("%1").arg(i+1*1000)));
    }




    //The view supports both dragging and dropping
    //设置这两句才能 拖拽复制覆盖
    ui->tableWidget->setDragDropMode(QAbstractItemView::DragDrop);
    ui->tableWidget->setDragEnabled(true);

    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//选中行为为选中一行

}
//行移动函数
void Widget::moveRow(QTableWidget *pTable, int currentRow, int toRow)
{
    if(pTable == NULL)
        return;

    int nRowCount = pTable->rowCount();
    int nColumnCount = pTable->columnCount();

    pTable->setFocus();

    if(currentRow == toRow)
        return;

    if(currentRow < 0 || toRow <0 ||currentRow > nRowCount || toRow > nRowCount)
        return;

    if(toRow < currentRow)
        currentRow++;

    pTable->insertRow(toRow);

    for(int i = 0; i <nColumnCount;i++)
    {
        pTable->setItem(toRow,i,pTable->takeItem(currentRow,i));
    }
    if(currentRow < toRow)
        toRow--;

    pTable->removeRow(currentRow);
    pTable->selectRow(toRow);

}
//上移一行
void Widget::on_moveUp_clicked()
{
    moveRow(ui->tableWidget,ui->tableWidget->currentRow(),ui->tableWidget->currentRow() - 1);
}
//下移一行
void Widget::on_moveDown_clicked()
{
    moveRow(ui->tableWidget,ui->tableWidget->currentRow(),ui->tableWidget->currentRow() + 2);
}
//删除一行
void Widget::on_remove_clicked()
{
    ui->tableWidget->removeRow(ui->tableWidget->currentRow());
}
        98年菜鸡一枚,请大佬们多多关照!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值