我是一个初学者,我正在开发一个工具,在这个工具中,用户可以在一个有2列的表格部件中重新排列文件行。
问题是当拖放一行时,目标会被删除。
我找到了一个解决方案。在QTableWidget中拖放行。
但我似乎无法在我的代码上使其工作。谁能告诉我应该从哪里开始?我不明白我应该在哪里添加布局和拖放事件类。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIntValidator, QDropEvent
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QTableWidget, QAbstractItemView, QTableWidgetItem, QWidget, QHBoxLayout, QApplication
from PyQt5.QtCore import QSettings, Qt
from pathlib import Path
import os
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(647, 564)
MainWindow.setAutoFillBackground(False)
MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(190, 130, 81, 31))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 127);")
self.pushButton_4.setObjectName("Browse")
self.pushButton_4.clicked.connect(self.browseFiles)
self.tableWidget_2 = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget_2.setGeometry(QtCore.QRect(300, 20, 321, 391))
self.tableWidget_2.setAutoFillBackground(True)
self.tableWidget_2.setFrameShape(QtWidgets.QFrame.Box)
self.tableWidget_2.setEditTriggers(QtWidgets.QAbstractItemView.AnyKeyPressed|QtWidgets.QAbstractItemView.DoubleClicked|QtWidgets.QAbstractItemView.EditKeyPressed|QtWidgets.QAbstractItemView.SelectedClicked)
self.tableWidget_2.setDragEnabled(True)
self.tableWidget_2.setAcceptDrops(True)
self.tableWidget_2.viewport().setAcceptDrops(True)
self.tableWidget_2.setDragDropOverwriteMode(False)
self.tableWidget_2.setDropIndicatorShown(True)
self.tableWidget_2.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.tableWidget_2.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.tableWidget_2.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
self.tableWidget_2.setAlternatingRowColors(True)
self.tableWidget_2.setIconSize(QtCore.QSize(5, 5))
self.tableWidget_2.setObjectName("tableWidget_2")
self.tableWidget_2.setColumnCount(2)
self.tableWidget_2.setRowCount(0)
self.tableWidget_2.setHorizontalHeaderLabels(["Filename","Page Count"])
self.tableWidget_2.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
self.pushButton_4.raise_()
self.tableWidget_2.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "Browse"))
def browseFiles(self):
self.pdfFiles = []
lastPath = ""
numRows = self.tableWidget_2.rowCount()
options = QFileDialog.Options()
# options |= QFileDialog.DontUseNativeDialog
files, _ = QFileDialog.getOpenFileNames(MainWindow,"Select File(s)", "","Files (*.*)", options=options)
for file in files:
self.tableWidget_2.insertRow(numRows)
self.tableWidget_2.setItem(numRows, 0, QtWidgets.QTableWidgetItem(file))
if len(self.pdfFiles) > 0:
lastPath = os.path.dirname(os.path.abspath(str(files)))
self.lineEdit_3.setText(lastPath)
self.disableButton()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
MainWindow.setWindowTitle("ARCMAP")
sys.exit(app.exec_())
```
修正了它。不得不推广我的表格小组件,并在这里导入TableWidgetDragRows类。在QTableWidget中拖放行。.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIntValidator, QDropEvent
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QTableWidget, QAbstractItemView, QTableWidgetItem, QWidget, QHBoxLayout, QApplication
from PyQt5.QtCore import QSettings, Qt
from pathlib import Path
import os
#import the tableWidgetDragRows in the link
from tablewidgetdragrows import TableWidgetDragRows
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(647, 564)
MainWindow.setAutoFillBackground(False)
MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(190, 130, 81, 31))
self.pushButton_4.setStyleSheet("background-color: rgb(0, 255, 127);")
self.pushButton_4.setObjectName("Browse")
self.pushButton_4.clicked.connect(self.browseFiles)
#edit the tableWidget from default to the imported sub class
self.tableWidget_2 = TableWidgetDragRows(self.centralwidget)
self.tableWidget_2.setGeometry(QtCore.QRect(300, 20, 321, 391))
self.tableWidget_2.setAutoFillBackground(True)
self.tableWidget_2.setFrameShape(QtWidgets.QFrame.Box)
self.tableWidget_2.setEditTriggers(QtWidgets.QAbstractItemView.AnyKeyPressed|QtWidgets.QAbstractItemView.DoubleClicked|QtWidgets.QAbstractItemView.EditKeyPressed|QtWidgets.QAbstractItemView.SelectedClicked)
#remove all drag and drop functions from your table widget so it would inherit from the imported subclass
# self.tableWidget_2.setDragEnabled(True)
# self.tableWidget_2.setAcceptDrops(True)
# self.tableWidget_2.viewport().setAcceptDrops(True)
# self.tableWidget_2.setDragDropOverwriteMode(False)
# self.tableWidget_2.setDropIndicatorShown(True)
# self.tableWidget_2.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
# self.tableWidget_2.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
# self.tableWidget_2.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
self.tableWidget_2.setAlternatingRowColors(True)
self.tableWidget_2.setIconSize(QtCore.QSize(5, 5))
self.tableWidget_2.setObjectName("tableWidget_2")
self.tableWidget_2.setColumnCount(2)
self.tableWidget_2.setRowCount(0)
self.tableWidget_2.setHorizontalHeaderLabels(["Filename","Page Count"])
self.tableWidget_2.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
self.pushButton_4.raise_()
self.tableWidget_2.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "Browse"))
def browseFiles(self):
self.pdfFiles = []
lastPath = ""
numRows = self.tableWidget_2.rowCount()
options = QFileDialog.Options()
# options |= QFileDialog.DontUseNativeDialog
files, _ = QFileDialog.getOpenFileNames(MainWindow,"Select File(s)", "","Files (*.*)", options=options)
for file in files:
self.tableWidget_2.insertRow(numRows)
self.tableWidget_2.setItem(numRows, 0, QtWidgets.QTableWidgetItem(file))
if len(self.pdfFiles) > 0:
lastPath = os.path.dirname(os.path.abspath(str(files)))
self.lineEdit_3.setText(lastPath)
self.disableButton()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
MainWindow.setWindowTitle("ARCMAP")
sys.exit(app.exec_())
```