QTreeWidget,拖拽节点时,需要点击两次,第一次无反应

6 篇文章 0 订阅

两个QTreeWidget,拖拽节点时,再次点击节点,需要点击两次,第一次无反应。
原因是还处于拖动状态,解决办法:

在树的点击事件中,增加去状态的代码:

void CTreeWidget::mousePressEvent(QMouseEvent* event)
{
	if (event->buttons() & Qt::LeftButton)
	{
		.....
	}
	resetState();//关键代码
	QTreeWidget::mousePressEvent(event);
}

void CTreeWidget::resetState()
{
	if (state() == DraggingState)
	{
		setState(NoState);
	}
}

参考:
QTreeWidget学习之拖拽节点后,需要点击两次节点才生效

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
这是因为在重新排列每一列位置的过程中,我们只是简单地使用了jQuery的insertAfter方法来移动每一列,但没有更新每一列的索引信息。因此,在第二次,索引信息已经失效,导致动无效。 为了解决这个问题,我们需要在重新排列每一列位置的同,更新每一列的索引信息。我们可以使用jQuery的each方法来遍历每一列,并更新它们的索引信息。 以下是修改后的代码,可支持多次动: ```html <!DOCTYPE html> <html> <head> <title>Table with colspan and draggable columns</title> <style type="text/css"> table { border-collapse: collapse; } th, td { padding: 10px; border: 1px solid black; } th { background-color: #EEE; cursor: move; user-select: none; } td[colspan="2"] { text-align: center; background-color: #DDD; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("th").draggable({ axis: "x", opacity: 0.7, helper: "clone", start: function(event, ui) { $(ui.helper).css("background-color", "#EEE"); }, stop: function(event, ui) { var index = $(this).index(); var newIndex = $(ui.helper).index(); if (newIndex > index) { newIndex -= $(ui.helper).attr("colspan") - 1; } $(this).closest("table").find("tr").each(function() { $(this).children("th:eq(" + index + ")").insertAfter($(this).children("th:eq(" + newIndex + ")")); $(this).children("td:eq(" + index + ")").insertAfter($(this).children("td:eq(" + newIndex + ")")); $(this).children("th").each(function(i) { $(this).attr("data-index", i); }); $(this).children("td").each(function(i) { $(this).attr("data-index", i); }); }); } }); }); </script> </head> <body> <table> <thead> <tr> <th colspan="2">Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> </tr> </thead> <tbody> <tr> <td colspan="2">Content 1</td> <td>Content 2</td> <td>Content 3</td> <td>Content 4</td> </tr> <tr> <td colspan="2">Content 5</td> <td>Content 6</td> <td>Content 7</td> <td>Content 8</td> </tr> <tr> <td colspan="2">Content 9</td> <td>Content 10</td> <td>Content 11</td> <td>Content 12</td> </tr> </tbody> </table> </body> </html> ``` 在修改后的代码中,我们在重新排列每一列位置的同,使用each方法来遍历每一列,并更新它们的索引信息。具体来说,我们为每一列添加了一个data-index属性,表示它的索引位置。这样,就可以支持多次动了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baibingql

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值