本文介绍了如何防止在 PySide 中使用 QTableWidget 删除项目时插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关帖子在这里

问题是当我将任意项目拖入其他项目时,如果我在项目的交叉点处放置,则会插入新行.

我只想更改数据.我不想插入新行或新列.

你有什么想法吗?

解决方案

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

为了做到这一点,

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.

这篇关于如何防止在 PySide 中使用 QTableWidget 删除项目时插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:43