当前位置:网站首页>QT筆記—— QTableWidget 之 拖拽行數 和 移動
QT筆記—— QTableWidget 之 拖拽行數 和 移動
2022-07-22 18:11:00 【舊街凉風°】
鏈接上一次QTableWidget 之 指定某列
首先我們需要允許拖拽:
//拖拽的模式
ui.tableWidget->setDragDropMode(QAbstractItemView::DragDrop);
//能否拖拽
ui.tableWidget->setDragEnabled(true);
成員函數:
public:
void moveRow(QTableWidget* table, int currentRow, int movetoRow); //指定某個錶格 從某行移動到某行
槽函數:
private slots:
void on_moveUpBtn_clicked(); //上移一個
void on_moveDownBtn_clicked(); //下移一個
void on_removeBtn_clicked(); //移除
void on_addBtn_clicked(); //增加
void Test::moveRow(QTableWidget* table, int currentRow, int movetoRow)
{
if (table == NULL)
return;
int rowCount = table->rowCount();
int colCount = table->columnCount();
//移動的距離超過錶格的範圍
if (movetoRow > rowCount || rowCount < 0 || movetoRow < 0 || currentRow < 0||colCount < 0)
return;
//******************** 第一種方式 *******************************
//移到的行數
if (movetoRow < currentRow) //上昇
{
//在需要一到的行數前增加一行
table->insertRow(movetoRow);
//獲取移動行的內容 並給新添加的行賦值原內容
for (int i = 0; i < colCount; i++)
{
QString str = table->item(currentRow + 1, i)->text();
QTableWidgetItem* item = new QTableWidgetItem;
item->setText(str);
table->setItem(movetoRow, i, item);
}
table->removeRow(currentRow + 1);
table->selectRow(movetoRow);
}
else if (movetoRow > currentRow) //下降
{
table->insertRow(movetoRow );
//獲取移動行的內容 並給新添加的行賦值原內容
for (int i = 0; i < colCount; i++)
{
QString str = table->item(currentRow, i)->text();
QTableWidgetItem* item = new QTableWidgetItem;
item->setText(str);
table->setItem(movetoRow, i, item);
}
table->removeRow(currentRow);
table->selectRow(movetoRow - 1);
}
********* 第二種方式 **********************
//上昇
//if (movetoRow < currentRow)
// currentRow++;
插入行
//table->insertRow(movetoRow);
//
向該行添加內容
//for (int i = 0; i < colCount; i++)
//{
// table->setItem(movetoRow, i, table->takeItem(currentRow, i));
//}
下降
//if (currentRow < movetoRow)
// movetoRow--;
//移除開始移動的行
//table->removeRow(currentRow);
//選擇的行數在移動的行
//table->selectRow(movetoRow);
}
void Test::on_moveUpBtn_clicked()
{
qDebug() << QStringLiteral("上昇");
int row = ui.tableWidget->currentRow();
moveRow(ui.tableWidget, row, row - 1);
}
void Test::on_moveDownBtn_clicked()
{
qDebug() << QStringLiteral("下降");
int row = ui.tableWidget->currentRow();
moveRow(ui.tableWidget, row, row + 2);
}
void Test::on_removeBtn_clicked()
{
int row = ui.tableWidget->currentRow();
if (row < 0)
return;
ui.tableWidget->removeRow(row);
}
void Test::on_addBtn_clicked()
{
int row = ui.tableWidget->currentRow();
if (row < 0)
return;
ui.tableWidget->insertRow(row + 1);
}
ui圖:
參考博客:
https://blog.csdn.net/weixin_42837024/article/details/82388077
边栏推荐
猜你喜欢
JS String charAt substring() substr slice toUpperCase toLowerCase indexOf
[case sharing] configure the routing penetration function of IS-IS
[bug] datetime format failed
在线XML转CSV工具
基础SQL Server 操作问题——仅当使用了列表并且IDENTITY_INSERT为ON时,才能为表中的标识列制定显示值
ieee下载文献的方法
Shallow solution of ZMQ lockless queue
RK3399平台开发系列讲解(内存篇)15.33、为什么可用内存会远超物理内存?
How to solve the gloomy life under the middle-aged crisis of it
MySQL数据库结合项目实战SQL优化总结
随机推荐
Gbase8s database minus operator
学点编程:防失业“疫苗”
十七、C函数指针与回调函数
融合注意力机制和BSRU的工业互联网安全态势预测方法
Gbase8s database set database object mode statement
MySQL 常用函数
力扣练习——28 拼接最大数
《微信小程序-进阶篇》Lin-ui组件库的安装与引入
济南 章丘 科目三 资料 收集
Evolution of multi activity in different places
异常的理解学习
Learn TB writing method with SPI simulation file
Gbase8s database set constraints statement
go 语言 结构体如何申明默认值 如何转化为json数据
GBase8s数据库INTERSECT 运算符
十七、C函數指針與回調函數
GBase8sUNION ALL 运算符
Go slice, set, simple explanation
JS String charAt substring() substr slice toUpperCase toLowerCase indexOf
Bigder:37/100 a misoperation