How to prevent from inserting new row when item is dropped with QTableWidget in PySide

微笑、不失礼 提交于 2019-12-11 08:35:01

问题


related post here

QTablewidget drop without creating new rows


but this has not been confirmed yet.

Is it duplicate?but I dare to ask...

I'm making QTableWidget

I want to install drag & drop Event.

But it has side-effect.

When this code is executed,

from PySide import QtGui
from PySide import QtCore
import sys

class CustomTableWidget(QtGui.QTableWidget):
    def __init__(self,row=0,column=0,parent=None):
        super(CustomTableWidget,self).__init__(parent=None)
        self.setRowCount(row)
        self.setColumnCount(column)      
        self.selection_start = False    
        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.setDragDropOverwriteMode(False)
        self.setDropIndicatorShown(True)

def main():
    try:
        QtGui.QApplication([])
    except Exception as e:

        print(e)
    table = CustomTableWidget(10,10)
    for i in range(10):
        for k in range(10):
            item = QtGui.QTableWidgetItem()
            item.setText("{0},{1}".format(i,k))
            table.setItem(i,k,item)
    table.show()
    sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
    main()

This is the shown widget.

The problem is when I drag an arbitrary item into other item,if I drop at the intersection of items,new row is inserted.

I want to change data only.I don't want to insert new row or column.

Do you have any idea?


回答1:


I don't know why it is,but I could do it.

For doing it,

self.setAcceptDrops(True)
self.setDragEnabled(True)

To confine the only two attributes achieves my purpose.



来源:https://stackoverflow.com/questions/54693219/how-to-prevent-from-inserting-new-row-when-item-is-dropped-with-qtablewidget-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!