拖拽 复制 覆盖 :
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 )));
}
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年菜鸡一枚,请大佬们多多关照!