当前位置:网站首页>QT笔记—— QTableWidget 之 拖拽行数 和 移动
QT笔记—— QTableWidget 之 拖拽行数 和 移动
2022-07-22 08:12: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
边栏推荐
- Gbase8s database union operator
- Bigder:38/100 a misoperation problem has been solved
- How to declare the default value in go language structure and how to convert it into JSON data
- Physical layer of network
- Rk3399 platform development series explanation (memory chapter) 15.33. Why does the available memory far exceed the physical memory?
- GBase8s数据库对 SET COLLATION 的限制
- Gbase8s database set connection statement
- 力扣练习——35 组合总和 II
- 17、 C function pointer and callback function
- 会议OA项目之会议发布功能
猜你喜欢
【3D目标检测】稀疏卷积
Dokcer运行Nacos容器自动退出问题
[3D target detection] sparse convolution
Detailed explanation of bokeh parameter setting
等额本金递增还款/等额本金递减按揭房贷还款计算器
[micro Service ~ remote call] integrate resttemplate, webclient, feign
What is the routing protocol
[opencv introduction practice] use the front camera of the computer for face detection
QT笔记——Qt动态属性 之 unpolish() 和 polish()
【案例分享】配置IS-IS的路由渗透功能
随机推荐
mysql 如何查询Json数据里面的数据
Rk3399 platform development series explanation (input subsystem) 4.52. Implementation principle of input subsystem
Fibonacci series of Niuke network
飞机大战中对象池模式子弹使用
Do all Navicat versions support MySQL? Why can't I open the connection?
How to declare the default value in go language structure and how to convert it into JSON data
vcs与verdi学习记录
QT笔记——Qt动态属性 之 unpolish() 和 polish()
Computer Graphics From Scratch - Chapter 4
Bigder:38/100 a misoperation problem has been solved
MySQL constraints
计算存款利息
GBase8sUNION ALL 运算符
Data link layer of network (PPP Protocol)
17、 C function pointer and callback function
[micro Service ~ remote call] integrate resttemplate, webclient, feign
prosody相关概念了解。xmpp,jabber,bosh等
Replace spaces in niuke.com
Concurrent model values actor and CSP
在线XML转CSV工具