Skip to content

Instantly share code, notes, and snippets.

@zhanglongqi
Created February 1, 2016 09:31
Show Gist options
  • Save zhanglongqi/9974d0989014dc4725ff to your computer and use it in GitHub Desktop.
Save zhanglongqi/9974d0989014dc4725ff to your computer and use it in GitHub Desktop.
Use QSlider in delegate way.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
longqi 21/Jan/16 14:35
"""
# This is only needed for Python v2 but is harmless for Python v3.
import sip
sip.setapi('QVariant', 2)
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt
class SpinBoxDelegate(QtGui.QItemDelegate):
def createEditor(self, parent, option, index):
editor = QtGui.QSlider(parent, orientation=Qt.Horizontal)
editor.setMinimum(0)
editor.setMaximum(100)
return editor
def setEditorData(self, slider, index):
value = index.model().data(index, QtCore.Qt.EditRole)
slider.setValue(value)
def setModelData(self, slider, model, index):
value = slider.value()
model.setData(index, value, QtCore.Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
model = QtGui.QStandardItemModel(4, 2)
tableView = QtGui.QTableView()
tableView.setModel(model)
delegate = SpinBoxDelegate()
tableView.setItemDelegate(delegate)
for row in range(4):
for column in range(2):
index = model.index(row, column, QtCore.QModelIndex())
model.setData(index, (row + 1) * (column + 1))
tableView.setWindowTitle("Spin Box Delegate")
tableView.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment