def dropMimeData(self, data, action, row, column, parent): if data.hasFormat("application/target.tableitem.creepy"): encodedData = data.data("application/target.tableitem.creepy") stream = QDataStream(encodedData, QIODevice.ReadOnly) columnsList = [] qVariant = QVariant() while not stream.atEnd(): stream >> qVariant columnsList.append(qVariant.toPyObject()) draggedRows = [columnsList[x : x + 5] for x in range(0, len(columnsList), 5)] droppedRows = [] for row in draggedRows: # Ensure we are not putting duplicates in the target list existed = False for target in self.targets: if row[2] == target["targetUsername"] and row[0] == target["pluginName"]: existed = True if not existed: droppedRows.append( { "targetUsername": row[2], "targetFullname": row[3], "targetPicture": row[1], "targetUserid": row[4], "pluginName": row[0], } ) self.insertRows(droppedRows, len(droppedRows), parent) return True
def dropMimeData(self, data, action, row, column, parent): if data.hasFormat('application/target.tableitem.creepy'): encodedData = data.data('application/target.tableitem.creepy') stream = QDataStream(encodedData, QIODevice.ReadOnly) columnsList = [] qVariant = QVariant() while not stream.atEnd(): stream >> qVariant columnsList.append(qVariant.toPyObject()) draggedRows = [ columnsList[x:x + 5] for x in range(0, len(columnsList), 5) ] droppedRows = [] for row in draggedRows: #Ensure we are not putting duplicates in the target list existed = False for target in self.targets: if row[2] == target['targetUsername'] and row[0] == target[ 'pluginName']: existed = True if not existed: droppedRows.append({ 'targetUsername': row[2], 'targetFullname': row[3], 'targetPicture': row[1], 'targetUserid': row[4], 'pluginName': row[0] }) self.insertRows(droppedRows, len(droppedRows), parent) return True
http://blog.csdn.net/fengyu09/article/details/37738193 pyqt中,要给QAbstractTableModel的setData函数传递一个list参数: [20,'00:00:19'] 涉及到QVariant和list的转换。 可以使用QVariant类中的toPyObject是转换。 环境是:Python 2.7.6 pyqt4 4.8.6 有文章说是,toPyObject只能转换字符串,而且只能转换字典。 测试一下,支持数字,支持字典和列表。 """ # coding:utf-8 from PyQt4.QtCore import QVariant a = {2: '10', 3: '00:00:09'} aa = QVariant(a) b = aa.toPyObject() print b print(b[2], b[3]) # 分割线 print('*' * 8) a = [20, '00:00:19'] aa = QVariant(a) b = aa.toPyObject() print b print(b[0], b[1])
class GraphicsWidget(RWidget): def __init__(self, parent): RWidget.__init__(self, parent) self.parent = parent self._hist = History(items=[]) self._currentPlot = QVariant(QString()) self.callLineEdit = QLineEdit() self.callLineEdit.setReadOnly(True) self.callLineEdit.setToolTip("Commands to reproduce plot") self.callLineEdit.setWhatsThis("Commands to reproduce plot") actions = [] # playAction = QAction("Replay", self) # playAction.setStatusTip("Replay selected plot") # playAction.setToolTip("Replay selected plot") # playAction.setIcon(QIcon(":edit-copy")) # actions.append(playAction) firstAction = QAction("End", self) firstAction.setStatusTip("Move to end of history") firstAction.setToolTip("Move to end of history") firstAction.setIcon(QIcon(":go-first")) actions.append(firstAction) previousAction = QAction("Previous", self) previousAction.setStatusTip("Move to previous plot") previousAction.setToolTip("Move to previous plot") previousAction.setIcon(QIcon(":go-previous")) actions.append(previousAction) infoAction = QAction("Info", self) infoAction.setStatusTip("Show plot info") infoAction.setToolTip("Show plot info") infoAction.setIcon(QIcon(":gtk-info")) actions.append(infoAction) nextAction = QAction("Next", self) nextAction.setStatusTip("Move to next plot") nextAction.setToolTip("Move to next plot") nextAction.setIcon(QIcon(":go-next")) actions.append(nextAction) lastAction = QAction("Start", self) lastAction.setStatusTip("Move to latest plot") lastAction.setToolTip("Move to latest plot") lastAction.setIcon(QIcon(":go-last")) actions.append(lastAction) actions.append(None) clearAction = QAction("Clear", self) clearAction.setStatusTip("Clear plot history") clearAction.setToolTip("Clear plot history") clearAction.setIcon(QIcon(":edit-clear")) actions.append(clearAction) hbox = QHBoxLayout() for action in actions: if not action is None: button = QToolButton() button.setDefaultAction(action) button.setAutoRaise(True) hbox.addWidget(button) else: hbox.addWidget(self.callLineEdit) self.setLayout(hbox) self.connect(firstAction, SIGNAL("triggered()"), self.first) self.connect(previousAction, SIGNAL("triggered()"), self.previous) self.connect(infoAction, SIGNAL("triggered()"), self.info) self.connect(nextAction, SIGNAL("triggered()"), self.next) self.connect(lastAction, SIGNAL("triggered()"), self.last) self.connect(clearAction, SIGNAL("triggered()"), self.clear) def setHistory(self, history): self._hist = history def history(self): return self._hist def previous(self): return self.navigateHistory(True) def next(self): return self.navigateHistory(False) def navigateHistory(self, up=True): if up: self._currentPlot = self.history().previous() else: self._currentPlot = self.history().next() tmp = self._currentPlot.toPyObject() if not tmp == QString(): self.callLineEdit.setText(tmp[2]) return self._currentPlot def first(self): temp = self.previous() item = self._currentPlot while not isinstance(temp,QVariant): item = temp temp = self.previous() self._currentPlot = item def last(self): temp = self.next() item = self._currentPlot while not isinstance(temp,QVariant): item = temp temp = self.next() self._currentPlot = item def info(self): plot = self._currentPlot.toPyObject() if isinstance(plot,QString): return plot = self._currentPlot.toPyObject() msgBox = QMessageBox() msgBox.setText("Plot information:") msgBox.setInformativeText("Original device: %s" % str(plot[0]) +"\nHistory position: %s" % str(self.history().currentIndex()+1) +"\nSize: %s bytes" % str(plot[1]) +"\n\nWould you like to replay the plot now?") msgBox.setDetailedText("Original call:\n%s" % plot[2]) msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Close) msgBox.setDefaultButton(QMessageBox.Ok) if msgBox.exec_() == QMessageBox.Ok: print plot[3] def clear(self): self.setHistory(History(items=[])) self.callLineEdit.setText(QString()) def updateHistory(self, item): self.history().update([item]) self.previous()