QTableView 拖动移动行 [英] QTableView drag move rows

查看:150
本文介绍了QTableView 拖动移动行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 QAbstractTableModelQSortFilterProxyModelQTableView 以便我可以通过单击表标题对项目进行排序.我想为用户添加选项,通过拖动它们来手动对视图中的行进行排序.我不需要能够从/向任何外部应用程序进行拖放操作,只需更改列表中的顺序即可.我也不需要更改模型中的数据,我只希望视图中的顺序不同.

I'm using a QTableView with a QAbstractTableModel and a QSortFilterProxyModel so that I can sort the items by clicking on the table headers. I'd like to add the option for the user to sort the rows in the view manually, by dragging them. I don't need to be able to do drag and drop from/to any external application, just to change the order in the list. I also don't need to change the data in the model, I only want the order to be different in the view.

我一直在查看文档,似乎我必须实现 mimeTypesmimeDatadropMimeData,但这快速变得非常复杂!我模型中的某些数据实际上并未显示在视图中,就像我说的那样,我不想更改模型中数据的顺序.有没有一种方法可以简单地拖动项目来改变它们的排序(就像标题已经能够做到的那样)而无需大量编码?

I've been looking through the documentation, and it seems like I have to implement mimeTypes, mimeData, and dropMimeData, but this gets very complicated fast! Some of the data in my model is not actually displayed in the view, and like I said I don't want to change the order of data in the model. Is there a way to simply drag items to change their sorting (just like the headers are already able to do) without a huge amount of coding?

推荐答案

针对 QT5 进行更新以移除不推荐使用的方法

如果您使用的是 PyQT,那么您需要做的就是满足您的要求:

If you are using PyQT All you need to do for your requirements is this:

your_tableview.verticalHeader().setSectionsMovable(True)
your_tableview.verticalHeader().setDragEnabled(True)
your_tableview.verticalHeader().setDragDropMode(QAbstractItemView.InternalMove)

然后冲洗并重复 horizo​​ntalHeader 如果您也希望那些可以重新排列.

Then rinse and repeat with horizontalHeader if you want those rearrange-able too.

您说得完全正确,您不需要接触甚至不知道此功能的模型是什么.您在模型本身上正确使用 QSortFilterProxyModel 装饰器可以进一步证明这一点.

You are absolutely right, you shouldn't need to touch or even know what the model is for this functionality. This is further demonstrated by your proper use of the QSortFilterProxyModel decorator over the model itself.

你看到的关于 mimeTypes 的东西和所有这些东西都是用于从其他窗口/应用程序/桌面/等拖放不同来源的实际对象,而不是你当前想要完成的任务.

The stuff you saw about mimeTypes and all of that stuff is for drag-and-drop of actual objects of varying sources from other windows/applications/desktop/etc and isn't needed for what you are trying to accomplish currently.

这篇关于QTableView 拖动移动行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆