且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止在 PySide 中使用 QTableWidget 删除项目时插入新行

更新时间:2023-12-03 21:13:46

我不知道为什么会这样,但我可以做到.

为了做到这一点,

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

限制仅有的两个属性达到了我的目的.

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?

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.