QTreeWidgetItem 上下平移

13 篇文章 1 订阅

思路就是获取到所在Group的所有child,放到QList中,重新换位置后再addChildren

void TabTreeWidget::downRow()
{
	QTreeWidgetItem *curritem = this->currentItem();
	if (curritem != NULL)
	{
		QTreeWidgetItem *curritemparent = curritem->parent();
		QTreeWidgetItem *curritem1;
		QList<QTreeWidgetItem*> childList;
		if (curritemparent == NULL ){return;}
		int index = curritemparent ->indexOfChild(curritem);
		int countChild = curritemparent ->childCount();
		if (index < countChild -1)
			{
				for (int i = 0; i < countChild; i++)
				{
					childList.push_back(curritemparent->child(i))
					if (curritemparent->child(i) == curritem)
						{
							curritem1 = curritemparent->child(i+1);
						}
				}
				curritemparent->takeChildren();
				childList[index] = curritem1;
				childList[index + 1] = curritem;		
				curritemparent->addChildren(childList);
			}
	}
}

void TabTreeWidget::upRow()
{
	QTreeWidgetItem *curritem = this->currentItem();
	if (curritem != NULL)
	{
		QTreeWidgetItem *curritemparent = curritem->parent();
		QTreeWidgetItem *curritem1;
		QList<QTreeWidgetItem*> childList;
		if (curritemparent == NULL ){return;}
		int index = curritemparent ->indexOfChild(curritem);
		int countChild = curritemparent ->childCount();
		if (index >0)
			{
				for (int i = 0; i < countChild; i++)
				{
					childList.push_back(curritemparent->child(i))
					if (curritemparent->child(i) == curritem)
						{
							curritem1 = curritemparent->child(i-1);
						}
				}
				curritemparent->takeChildren();
				childList[index] = curritem1;
				childList[index - 1] = curritem;		
				curritemparent->addChildren(childList);
			}
	}
}



174行
menu.addAction(QStringLiteral("上移一层"), this, SLOT(upRow()));
menu.addAction(QStringLiteral("下移一层"), this, SLOT(downRow()));



头文件
slot:
void upRow()void downRow()
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用QTreeWidget的insertTopLevelItem()和takeTopLevelItem()方法来实现QTreeWidgetItem的上下移动。具体步骤如下: 1. 获取当前选中的QTreeWidgetItem对象。 2. 判断当前选中的QTreeWidgetItem对象是否为顶级项目(即没有父项目),如果不是,则不能进行上下移动。 3. 获取当前选中的QTreeWidgetItem对象在QTreeWidget中的索引。 4. 如果要将当前选中的QTreeWidgetItem对象向上移动,则将其从QTreeWidget中取出,并将其插入到索引值减1的位置。 5. 如果要将当前选中的QTreeWidgetItem对象向下移动,则将其从QTreeWidget中取出,并将其插入到索引值加1的位置。 示例代码如下: ``` # 获取当前选中的QTreeWidgetItem对象 selected_item = self.treeWidget.currentItem() # 判断当前选中的QTreeWidgetItem对象是否为顶级项目 if selected_item.parent() is None: # 获取当前选中的QTreeWidgetItem对象在QTreeWidget中的索引 index = self.treeWidget.indexOfTopLevelItem(selected_item) # 如果要将当前选中的QTreeWidgetItem对象向上移动 if direction == 'up': # 将其从QTreeWidget中取出 item = self.treeWidget.takeTopLevelItem(index) # 将其插入到索引值减1的位置 self.treeWidget.insertTopLevelItem(index - 1, item) # 如果要将当前选中的QTreeWidgetItem对象向下移动 elif direction == 'down': # 将其从QTreeWidget中取出 item = self.treeWidget.takeTopLevelItem(index) # 将其插入到索引值加1的位置 self.treeWidget.insertTopLevelItem(index + 1, item) ``` 注意:在实际使用中,需要根据具体情况进行修改和适配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值