在 PyQT4 中上下移动行
考虑一个 QTableWidget 和两个按钮“上移”和“下移”。单击“上移”,当前行应上移一行,类似于“下移”。
实现相应的上移和下移功能的最简单方法是什么?
Consider a QTableWidget and two buttons "move up" and "move down". Clicking on move up, the current row should move up one row, analogously for "move down".
What's the easiest way to implement the corresponding move up and move down functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法仅使用 QTableWidget 来完成此操作,这是一个完整的示例:
I managed to do it using only
QTableWidget
, here is a full example:我修改了我的答案,因为它之前没有足够的细节
该过程涉及将按钮连接到一个或多个插槽,该插槽将查看当前选择,并通过从视图中取出它们来移动它们,并将它们插入到新位置。
下面的例子实际上是使用QTableView + QStandardItemModel。原因是因为 QTableWidget 受到更多限制,因为您只能使用小部件中的方法。能够直接使用模型和选择模型要容易得多。尽管如此,如果您多次使用
takeItem()
来构建每一行,则可以为 QTableWidget 重新编写此示例...这是一个完整的工作示例:
init 很简单,只需设置表、模型和按钮。我们使用 functools.partial 将两个按钮连接到相同的方法,这对于使用不同参数包装相同的函数调用非常方便。那么表格就充满了 20x6 的数据。
单击按钮时,我们确保他们选择了行。对于每个选定的行,我们解析其项目(以便在移动后重新选择),并通过加或减 1 来确定新的行号。我们还要确保它在有效的移动范围内,否则我们会跳过它。最后,我们调用
takeRow()
将整行作为索引列表删除,然后将该行插入新的行号。在该循环之后,我们使用保存的项目来查找新索引并再次重新选择它们。I have revised my answer because it did not have enough detail previously
The process involves connecting your buttons to a slot (or slots) that will look at the current selection, and move them by taking them from the view, and inserting them into new locations.
The following example is actually using a QTableView + QStandardItemModel. The reason is because QTableWidget is much more limited since you can only use methods from the widget. Its a lot easier to be able to work directly with the model and selection model. Although, it is possible to rework this example for a QTableWidget if you use
takeItem()
multiple times to build up each row...Here is a fully working example:
The init is simple and just sets up the table, model, and buttons. We connect both buttons to the same method using
functools.partial
, which is really convenient for wrapping up the same function call with different args. Then the table is just filled with 20x6 data.When a button is clicked, we make sure they have selected rows. For each selected row, we resolve its item (for re-selection later once it has moved), and determine the new row number by either adding or subtracting one. We also make sure its within the valid range to move, otherwise we skip it. Finally, we call
takeRow()
to remove the entire row as a list of indexes, and then insert that row back into the new row number. After that loop, we use the items we saved to look up the new indexes and reselect them again.