我正在尝试编写一个小应用程序,并希望以某种方式“捕获”拖动/删除行但但对表没有影响的行号。我创建了两个文件,第一个是在Qt Designer中为基本GUI创建的MainWindow UI文件,第二个是使用一些数据来布局QTableWidget的代码。
它按照我的意愿工作,也就是说它向用户显示拖放动作,但在删除时不会改变任何内容。我已阅读其他帖子,但他们似乎都想要移动数据,因此他们对此没有任何帮助。
UiFile.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'UiFile.ui'
#
# Created: Thu Apr 7 21:07:11 2016
# by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(763, 269)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setDragEnabled(False)
self.tableWidget.setDragDropOverwriteMode(False)
self.tableWidget.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.tableWidget.setDefaultDropAction(QtCore.Qt.ActionMask)
self.tableWidget.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
self.tableWidget.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(7)
self.tableWidget.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(5, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(6, item)
self.gridLayout.addWidget(self.tableWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 763, 24))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Column 1", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Column 2", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Column 3", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Column 4", None))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Column 5", None))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "New Column", None))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "Column 7", None))
main.py
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
import UiFile
class ExampleApp(QtGui.QMainWindow, UiFile.Ui_MainWindow):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
self.home()
def home(self):
self.tableWidget.itemChanged.connect(self.my_item_changed_info)
self.add_row_to_ml_table("R1C1", "R1C2", "R1C3", "R1C4", "R1C5", "R1C6", "R1C7")
self.add_row_to_ml_table("R2C1", "R2C2", "R2C3", "R2C4", "R2C5", "R2C6", "R2C7")
self.add_row_to_ml_table("R3C1", "R3C2", "R3C3", "R3C4", "R3C5", "R3C6", "R3C7")
self.add_row_to_ml_table("R4C1", "R4C2", "R4C3", "R4C4", "R4C5", "R4C6", "R4C7")
self.add_row_to_ml_table("R5C1", "R5C2", "R5C3", "R5C4", "R5C5", "R5C6", "R5C7")
self.add_row_to_ml_table("R6C1", "R6C2", "R6C3", "R6C4", "R6C5", "R6C6", "R6C7")
self.add_row_to_ml_table("R7C1", "R7C2", "R7C3", "R7C4", "R7C5", "R7C6", "R7C7")
def add_row_to_ml_table(self, c1, c2, c3, c4, c5, c6, c7):
rowposition = self.tableWidget.rowCount()
self.tableWidget.insertRow(rowposition)
self.tableWidget.setItem(rowposition, 0, QtGui.QTableWidgetItem(c1))
self.tableWidget.setItem(rowposition, 1, QtGui.QTableWidgetItem(c2))
self.tableWidget.setItem(rowposition, 2, QtGui.QTableWidgetItem(c3))
self.tableWidget.setItem(rowposition, 3, QtGui.QTableWidgetItem(c4))
self.tableWidget.setItem(rowposition, 4, QtGui.QTableWidgetItem(c5))
self.tableWidget.setItem(rowposition, 5, QtGui.QTableWidgetItem(c6))
self.tableWidget.setItem(rowposition, 6, QtGui.QTableWidgetItem(c7))
def my_item_changed_info(self, something):
if self.tableWidget.currentRow() != -1:
print("current", self.tableWidget.currentRow(), self.tableWidget.currentColumn())
print(something.row(), something.column())
def closeEvent(self, event):
choice = QtGui.QMessageBox.question(self, 'Xmms2 Skin', 'Do you want to quit the applictaion?', QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if choice == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
所以我需要做的是获取已拖动的行的信息以及被删除的行。
根据我对此的了解,我必须创建一个QTableWidget的子类才能捕获事件。
我可以使用Qt Designer吗?我根据自己的编程技巧首先选择了这个,这是一个更复杂的GUI布局的一小部分。
答案 0 :(得分:0)
我自己通过添加
解决了这个问题self.tableWidget.__class__.dropEvent = self.testdropfunc
进入home函数,然后testdropfunc看起来像这样: -
def testdropfunc(self, event):
moved_from = self.tableWidget.currentRow()
moved_to = self.tableWidget.rowAt(event.pos().y())
print("Drop from " + str(moved_from) + " into row " + str(moved_to))
event.accept()
我确实意识到这实际上并没有移动任何东西,因为我想要的只是项目的行信息("从#34拖出;"拖到")