当前位置:网站首页>QT notes - drag lines and movement of qtablewidget
QT notes - drag lines and movement of qtablewidget
2022-07-22 18:11:00 【Cool breeze in the old street °】
Link last time QTableWidget And Specify a column
First, we need to allow dragging :
// Drag and drop mode
ui.tableWidget->setDragDropMode(QAbstractItemView::DragDrop);
// Can I drag
ui.tableWidget->setDragEnabled(true);
Member functions :
public:
void moveRow(QTableWidget* table, int currentRow, int movetoRow); // Specify a table Move from a row to a row
Slot function :
private slots:
void on_moveUpBtn_clicked(); // Move up one
void on_moveDownBtn_clicked(); // Move one down
void on_removeBtn_clicked(); // remove
void on_addBtn_clicked(); // increase
void Test::moveRow(QTableWidget* table, int currentRow, int movetoRow)
{
if (table == NULL)
return;
int rowCount = table->rowCount();
int colCount = table->columnCount();
// The moving distance exceeds the range of the table
if (movetoRow > rowCount || rowCount < 0 || movetoRow < 0 || currentRow < 0||colCount < 0)
return;
//******************** The first way *******************************
// Number of rows moved to
if (movetoRow < currentRow) // rising
{
// Add one line before the number of lines that need one to
table->insertRow(movetoRow);
// Get the contents of the mobile line And assign the original content to the newly added row
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) // falling
{
table->insertRow(movetoRow );
// Get the contents of the mobile line And assign the original content to the newly added row
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);
}
********* The second way **********************
// rising
//if (movetoRow < currentRow)
// currentRow++;
Insert row
//table->insertRow(movetoRow);
//
Add content to this line
//for (int i = 0; i < colCount; i++)
//{
// table->setItem(movetoRow, i, table->takeItem(currentRow, i));
//}
falling
//if (currentRow < movetoRow)
// movetoRow--;
// Remove the line that started moving
//table->removeRow(currentRow);
// The number of selected rows is in the moving row
//table->selectRow(movetoRow);
}
void Test::on_moveUpBtn_clicked()
{
qDebug() << QStringLiteral(" rising ");
int row = ui.tableWidget->currentRow();
moveRow(ui.tableWidget, row, row - 1);
}
void Test::on_moveDownBtn_clicked()
{
qDebug() << QStringLiteral(" falling ");
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 chart :
Reference blog :
https://blog.csdn.net/weixin_42837024/article/details/82388077
边栏推荐
- 需要达到什么水平,程序员才能顺利拿到20k月薪无压力?
- Gbase8s database minus operator
- pushgateway安装及Prometheus配置
- Gbase8s database set constraints statement
- 逻辑回归(公式推导+numpy实现)
- Understanding and learning of arrays class
- [paper summary] 2D target detection article summary, continuously updated
- MySQL 常用函数
- [3D target detection] sparse convolution
- Aruba学习笔记04-Web UI --Configuration面板介绍
猜你喜欢
VCs and Verdi learning records
Bigder:38/100 a misoperation problem has been solved
在线XML转CSV工具
Go concurrent programming - work pool
What is the routing protocol
[opencv introduction practice] use the front camera of the computer for face detection
【3D目标检测】稀疏卷积
Linear regression (formula derivation +numpy Implementation)
如何破解IT中年危机下的惨淡人生
QT笔记——自定义数据类型
随机推荐
go 语言 结构体如何申明默认值 如何转化为json数据
Gbase8s database set autofree statement
Abnormal understanding and learning
Data link layer of network (PPP Protocol)
Logistic regression (formula derivation +numpy Implementation)
[LeetCode]-链表-3
QT笔记——QtXml
基于信誉管理模型的矿工类型鉴别机制设计
JS String charAt substring() substr slice toUpperCase toLowerCase indexOf
MySQL和MariaDB区别
Detailed explanation of bokeh parameter setting
[exception] generate guid and datetime, import test data to the database
web3分享
Hblock盘活企业级存储市场
How Allegro imports pictures such as high-definition logo, QR code, anti-static logo and Chinese characters
地图找房的实例
OceanBase数据库搭建测试
【Excle】生成guid和datetime导入测试数据到数据库
Calculate deposit interest
以SPI的仿真文件学习TB写法