Skip to content

Instantly share code, notes, and snippets.

@fereria
Created July 31, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fereria/9162b3e7afa4691a842eda748cebf910 to your computer and use it in GitHub Desktop.
Save fereria/9162b3e7afa4691a842eda748cebf910 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from PySide import QtCore, QtGui
import os.path
from ui import table_view
CURRENT_PATH = os.path.dirname(__file__)
class TableView(QtGui.QDialog):
def __init__(self, parent=None):
super(TableView, self).__init__(parent)
self.ui = table_view.Ui_Form()
self.ui.setupUi(self)
# init items
self.model = CustomTableModel()
self.ui.tableView.setModel(self.model)
self.delegate = SpinBoxDelegate()
self.ui.tableView.setItemDelegate(self.delegate)
self.ui.print_select_items.clicked.connect(self.clicked)
self.model.set_items(TableItem(False, 30))
self.model.set_items(TableItem(False, 40))
self.model.set_items(TableItem(False, 50))
def clicked(self):
print self.model.get_select_data()
class TableItem(object):
check = 0
per = 0
def __init__(self, check, per):
self.check = check
self.per = per
class CustomTableModel(QtCore.QAbstractTableModel):
__items = []
def __init__(self, parent=None):
super(CustomTableModel, self).__init__(parent=None)
def rowCount(self, index=QtCore.QModelIndex()):
return len(self.__items)
def columnCount(self, index=QtCore.QModelIndex()):
return 2
def setCheckBox(self, index, value):
self.__items[index.row()].check = value
def data(self, index, role=QtCore.Qt.DisplayRole):
if not index.isValid():
return None
if role == QtCore.Qt.DisplayRole:
if index.column() == 0:
return self.__items[index.row()].check
else:
return self.__items[index.row()].per
def set_items(self, data):
self.__items.append(data)
def get_select_data(self):
return [x for x in self.__items if x.check is True]
class SpinBoxDelegate(QtGui.QItemDelegate):
def createEditor(self, parent, option, index):
# ダブルクリックで呼ばれるWidgetを設定
if index.column == 1:
editor = QtGui.QDoubleSpinBox(parent)
editor.setMinimum(0)
editor.setMaximum(100)
editor.setValue(index.data())
return editor
def editorEvent(self, event, model, option, index):
rect = QtCore.QRect(option.rect.x() + (option.rect.width() / 2) - (option.rect.height() / 2),
option.rect.top(),
option.rect.height(),
option.rect.height())
if rect.contains(event.pos().x(), event.pos().y()) is True:
# CheckBoxの6処理
if index.column() == 0:
if event.type() == QtCore.QEvent.MouseButtonPress:
if index.data():
model.setCheckBox(index, False)
else:
model.setCheckBox(index, True)
return True
def setEditorData(self, editor, index):
# ダブルクリックしてEdit状態になったときに呼ばれる
value = index.model().data(index, QtCore.Qt.EditRole)
editor.setValue(value)
def setModelData(self, editor, model, index):
# Edit状態から通常状態になった時に呼ばれる
editor.interpretText()
value = editor.value()
model.setData(index, value, QtCore.Qt.EditRole)
def paint(self, painter, option, index):
"""
TableViewのCell内の描画を行う
"""
# CheckBoxのようなものを描画
if index.column() == 0:
rect = QtCore.QRect(option.rect.x() + (option.rect.width() / 2) - (option.rect.height() / 2),
option.rect.top(),
option.rect.height(),
option.rect.height())
icon = "check_off.png"
if index.data() == 1:
icon = "check_on.png"
img = QtGui.QPixmap(CURRENT_PATH + "/" + icon)
painter.drawPixmap(rect, img)
# ProgressBarを表示
if index.column() == 1:
bar = QtGui.QStyleOptionProgressBar()
bar.rect = option.rect
bar.rect.setHeight(option.rect.height() - 1)
bar.rect.setTop(option.rect.top() + 1)
bar.minimum = 0
bar.maximum = 100
bar.progress = int(index.data())
bar.textVisible = True
bar.text = str(index.data()) + '%'
bar.textAlignment = QtCore.Qt.AlignCenter
QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_ProgressBar, bar, painter)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
ui = TableView()
ui.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment