pyqt5覆盖dropEvent python

时间:2018-11-01 16:18:03

标签: python pyqt5 qtablewidget qlistwidget

我正在尝试向小型应用程序添加拖放功能。从QlistWidget获取数据并将数据拖放到QTableWidget上。我应该重写QTableWidget的dropEvent,以便在删除数据时添加其他功能。但是我有麻烦,我想我无法从ListWidget获得对象的text()。这是代码:

class Table(QtWidgets.QTableWidget):
    def __init__(self,r,c, parent=None):
        super().__init__(r,c,parent)    
        self.init_ui()

    def init_ui(self):            
        self.setAcceptDrops(True)
        self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)

    """def dragMoveEvent(self, e):            
        e.setDropAction(QtCore.Qt.MoveAction)
        e.accept()

    def dragEnterEvent(self,e):            
        e.accept()"""      



    def dropEvent(self,e):            
        data = e.mimeData()
        a=e.pos()
        row = self.rowAt(a.y())
        col = self.columnAt(a.x())
        self.setItem(row,col,QtWidgets.QTableWidgetItem(data.text()))

        print(row,col)
        print(type(data.text()))
        print(e.source())
        x = data.text()
        print(x)
        e.accept()
`

1 个答案:

答案 0 :(得分:0)

QListWidget不会通过text()传递从application/x-qabstractitemmodeldatalist传输的数据,因为一项具有由角色标识的更多信息,此外您还可以拖动多个项目。数据使用MIME类型from PyQt5 import QtCore, QtWidgets class TableWidget(QtWidgets.QTableWidget): def __init__(self, r,c, parent=None): super(TableWidget, self).__init__(r,c, parent) self.setAcceptDrops(True) self.setDragDropMode(QtWidgets.QAbstractItemView.DropOnly) def dropEvent(self, event): md = event.mimeData() fmt = "application/x-qabstractitemmodeldatalist" if md.hasFormat(fmt): encoded = md.data(fmt) stream = QtCore.QDataStream(encoded, QtCore.QIODevice.ReadOnly) table_items = [] while not stream.atEnd(): # row and column where it comes from row = stream.readInt32() column = stream.readInt32() map_items = stream.readInt32() it = QtWidgets.QTableWidgetItem() for i in range(map_items): role = stream.readInt32() value = QtCore.QVariant() stream >> value it.setData(role, value) table_items.append(it) for it in table_items: print(it, it.text()) class MainWindow(QtWidgets.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) list_widget = QtWidgets.QListWidget() list_widget.setAcceptDrops(False) list_widget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) list_widget.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly) for i in range(10): it = QtWidgets.QListWidgetItem("item-{}".format(i)) list_widget.addItem(it) table_widget = TableWidget(5, 10) central_widget = QtWidgets.QWidget() hlay = QtWidgets.QHBoxLayout(central_widget) hlay.addWidget(list_widget) hlay.addWidget(table_widget) self.setCentralWidget(central_widget) if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) w = MainWindow() w.show() sys.exit(app.exec_()) 进行传输,解决方案如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "minimumTemperature") as! UITableViewCell

    let data = myArrayOfDays[indexPath.section]
    let temp = indexPath.row == 0 ? data.minimumTemp : data.maximumTemp
    cell.textLabel?.text = "\(temp)"

    return cell
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return myArrayOfDays.count
}