gpt4 book ai didi

python - 在两个单独的 QTableWidgets 之间拖放行

转载 作者:行者123 更新时间:2023-11-28 18:33:38 25 4
gpt4 key购买 nike

背景:

我的主窗口上有两个独立的QTableWidget。两者都是在python中动态添加的。我已经实现了pyqt代码,可以在three_pineapples解决方案下找到here .

我的问题

我希望能够将表格行从一个表格拖放到另一个表格。例如:

如果我试图将项目 B 从左表拖到右表

Example pt. 1

结果是这样

Example pt. 2

关于如何获得这种期望的行为有什么建议吗?

最佳答案

看了@three_pineapples 的代码,一半没看懂,删了一半。适用于多行选择。

import sys
from PyQt4.QtGui import *

class TableWidgetDragRows(QTableWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setDragDropOverwriteMode(False)
# self.setSelectionMode(QAbstractItemView.SingleSelection)

self.last_drop_row = None

# Override this method to get the correct row index for insertion
def dropMimeData(self, row, col, mimeData, action):
self.last_drop_row = row
return True


def dropEvent(self, event):
# The QTableWidget from which selected rows will be moved
sender = event.source()

# Default dropEvent method fires dropMimeData with appropriate parameters (we're interested in the row index).
super().dropEvent(event)
# Now we know where to insert selected row(s)
dropRow = self.last_drop_row

selectedRows = sender.getselectedRowsFast()

# Allocate space for transfer
for _ in selectedRows:
self.insertRow(dropRow)

# if sender == receiver (self), after creating new empty rows selected rows might change their locations
sel_rows_offsets = [0 if self != sender or srow < dropRow else len(selectedRows) for srow in selectedRows]
selectedRows = [row + offset for row, offset in zip(selectedRows, sel_rows_offsets)]

# copy content of selected rows into empty ones
for i, srow in enumerate(selectedRows):
for j in range(self.columnCount()):
item = sender.item(srow, j)
if item:
source = QTableWidgetItem(item)
self.setItem(dropRow + i, j, source)

# delete selected rows
for srow in reversed(selectedRows):
sender.removeRow(srow)

event.accept()


def getselectedRowsFast(self):
selectedRows = []
for item in self.selectedItems():
if item.row() not in selectedRows:
selectedRows.append(item.row())
selectedRows.sort()
return selectedRows


class Window(QWidget):
def __init__(self):
super().__init__()

layout = QHBoxLayout()
self.setLayout(layout)

self.table_widgets = []
for _ in range(3):
tw = TableWidgetDragRows()
tw.setColumnCount(2)
tw.setHorizontalHeaderLabels(['Colour', 'Model'])

self.table_widgets.append(tw)
layout.addWidget(tw)

filled_widget = self.table_widgets[0]
items = [('Red', 'Toyota'), ('Blue', 'RV'), ('Green', 'Beetle')]
for i, (colour, model) in enumerate(items):
c = QTableWidgetItem(colour)
m = QTableWidgetItem(model)

filled_widget.insertRow(filled_widget.rowCount())
filled_widget.setItem(i, 0, c)
filled_widget.setItem(i, 1, m)


if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

关于python - 在两个单独的 QTableWidgets 之间拖放行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34533878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com