Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt4.QtCore import QAbstractTableModel, QModelIndex, pyqtSignal, pyqtSlot, QPoint, Qt
- from PyQt4.QtGui import ( QTableView, QItemDelegate, QComboBox,
- QLineEdit, QToolButton, QHBoxLayout, QFileDialog,
- QStyle, QRegion, QWidget, QVBoxLayout,
- QApplication, QMouseEvent)
- class TableModel(QAbstractTableModel):
- def rowCount(self, parent=QModelIndex()):
- return 5
- def columnCount(self, parent=QModelIndex()):
- return 4
- def data(self, index, role = Qt.DisplayRole):
- if index.isValid() and role == Qt.DisplayRole:
- return "{0:02d}".format(index.row())
- return None
- def setData(self, index, value, role = Qt.DisplayRole):
- print "setData", index.row(), index.column(), value
- def flags(self, index):
- return Qt.ItemIsEditable | Qt.ItemIsEnabled
- class TableView(QTableView):
- def mousePressEvent(self, event):
- if event.button() == Qt.LeftButton:
- index = self.indexAt(event.pos())
- if index.isValid():
- self.setCurrentIndex(index)
- self.edit(index) #allow one-click editing
- else:
- super(TableView, self).mousePressEvent(event)
- class ComboDelegate(QItemDelegate):
- def __init__(self, parent):
- super(ComboDelegate, self).__init__(parent)
- #gotta provide a parent to the helper to get the correct style
- self._helperCombo = QComboBox(parent)
- self._helperCombo.setVisible(False)
- def createEditor(self, parent, option, index):
- return QComboBox(parent)
- def setEditorData(self, editor, index):
- editor.addItems( ["00", "01", "02", "03", "04"] )
- editor.showPopup() #allow one-click editing, but only for comboBoxes :(
- def setModelData(self, editor, model, index):
- model.setData(index, editor.currentIndex())
- def paint(self, painter, option, index):
- # Draws the _helperCombo as the background
- painter.save()
- painter.translate(option.rect.topLeft())
- self._helperCombo.resize(option.rect.size())
- self._helperCombo.render(painter, QPoint(), QRegion(), QWidget.DrawChildren)
- painter.restore()
- option.rect.adjust(1,1,-1,-1) # because the widget fits inside the cell
- super(ComboDelegate, self).paint(painter, option, index)
- class FilePickerWidget( QWidget ):
- def __init__( self, parent=None ):
- super(FilePickerWidget, self).__init__(parent)
- self.uiFilenameTXT = QLineEdit(self)
- self.uiPickFileBTN = QToolButton(self)
- self.uiPickFileBTN.setText('...')
- layout = QHBoxLayout(self)
- layout.addWidget(self.uiFilenameTXT)
- layout.addWidget(self.uiPickFileBTN)
- layout.setContentsMargins(0, 0, 0, 0)
- self.setLayout(layout)
- self.uiPickFileBTN.clicked.connect( self.pickPath )
- def filePath( self ):
- return self.uiFilenameTXT.text()
- def pickPath( self ):
- filepath = QFileDialog.getOpenFileName( self, "Pick File...", self.uiFilenameTXT.text(), "All Files (*.*)")
- if filepath:
- self.uiFilenameTXT.setText( filepath )
- self.emitFilenamePicked()
- def setFilePath( self, filePath ):
- self.uiFilenameTXT.setText( filePath )
- ### THIS IS THE DELEGATE THAT I NEED HELP WITH
- class FilePickerDelegate(QItemDelegate):
- def __init__(self, parent):
- super(FilePickerDelegate, self).__init__(parent)
- #gotta set the parent of the helper to get the correct style
- self._helperPicker = FilePickerWidget(parent)
- self._helperPicker.setVisible(False)
- def createEditor(self, parent, option, index):
- return FilePickerWidget(parent)
- def setEditorData(self, editor, index):
- editor.setFilePath(r'01')
- def setModelData(self, editor, model, index):
- model.setData(index, editor.filePath())
- def paint(self, painter, option, index):
- # Draws the self._helperPicker as the background
- painter.save()
- painter.translate(option.rect.topLeft())
- self._helperPicker.resize(option.rect.size())
- self._helperPicker.render(painter, QPoint(), QRegion(), QWidget.DrawChildren)
- painter.restore()
- #continue on with the regularly scheduled program
- super(FilePickerDelegate, self).paint(painter, option, index)
- def editorEvent(self, event, model, option, index):
- if event.type() & event.MouseButtonPress:
- #recreate what happens normally with the delegate
- par = self.parent().viewport()
- ed = self.createEditor(par, option, index)
- self.setEditorData(ed, index)
- pos = event.pos() - option.rect.topLeft()
- ev = QMouseEvent(event.type(), pos, event.button(), event.buttons(), event.modifiers())
- #ed.mousePressEvent(ev) ### Doesn't work
- return True
- return super(FilePickerDelegate, self).editorEvent(event, model, option, index)
- class TestWidget(QWidget):
- def __init__(self, parent=None):
- QWidget.__init__(self, parent)
- self.tableModel = TableModel(self)
- self.table = TableView(self)
- self.table.setItemDelegate(ComboDelegate(self.table))
- self.table.setItemDelegateForColumn(3, FilePickerDelegate(self.table))
- self.table.setMouseTracking(True)
- self.table.setModel(self.tableModel)
- #self.table.setEditTriggers(self.table.AllEditTriggers)
- l = QVBoxLayout(self)
- l.addWidget(self.table)
- if __name__ == "__main__":
- from sys import argv, exit
- a = QApplication(argv)
- w = TestWidget()
- w.show()
- w.raise_()
- exit(a.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement