Last active
August 29, 2015 14:16
-
-
Save fereria/6531110dfc5e4a51f9ca to your computer and use it in GitHub Desktop.
Get ChangeValue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## -*- coding: utf-8 -*- | |
#model/viewの基本 | |
import sys | |
from PySide import QtCore, QtGui | |
from PySide.QtUiTools import QUiLoader | |
""" | |
編集情報を取得してプリントするテスト | |
""" | |
class testView(QtGui.QDialog): | |
def __init__(self,parent=None): | |
super(testView,self).__init__(parent) | |
layout = QtGui.QVBoxLayout() | |
self.setLayout(layout) | |
self.view = QtGui.QListView() | |
layout.addWidget(self.view) | |
#中のデータ(モデル)を作成して、Viewにセット | |
self.model = QtGui.QStringListModel(['a','b','c']) | |
self.view.setModel(self.model) | |
#編集情報を取得するためのDelegate作成 | |
self.dele = TestDelegate() | |
self.dele.changeValue.connect(self.printChangeValue) | |
self.view.setItemDelegate(self.dele) | |
def printChangeValue(self,src,dst): | |
print "------------" | |
print src | |
print dst | |
print "------------" | |
class TestDelegate(QtGui.QItemDelegate): | |
changeValue = QtCore.Signal(str,str) | |
def createEditor(self, parent, option, index): | |
#編集する時に呼ばれるWidgetを設定 | |
editor = QtGui.QLineEdit(parent) | |
return editor | |
def setEditorData(self, LineEdit, index): | |
#編集されたときに呼ばれ、セットされた値をWidgetにセットする? | |
value = index.model().data(index, QtCore.Qt.EditRole) | |
LineEdit.setText(value) | |
def setModelData(self, LineEdit, model, index): | |
#ModelにSpinboxの編集した値をセットする? | |
value = LineEdit.text() | |
#編集情報をSignalで発信する | |
self.changeValue.emit(index.model().data(index,QtCore.Qt.EditRole),value) | |
model.setData(index, value, QtCore.Qt.EditRole) | |
def updateEditorGeometry(self, editor, option, index): | |
editor.setGeometry(option.rect) | |
def paint(self,painter,option,index): | |
#通常時の表示 | |
num = index.data() | |
#選択時は背景色変更 | |
painter.setPen(QtGui.QPen(QtCore.Qt.NoPen)) | |
if option.state & QtGui.QStyle.State_Selected: | |
painter.setBrush(QtGui.QBrush(QtCore.Qt.darkCyan)) | |
painter.drawRect(option.rect) | |
#文字表示 | |
painter.setPen(QtGui.QPen(QtCore.Qt.black)) | |
painter.drawText(option.rect, QtCore.Qt.AlignLeft, str(num)) | |
#アプリケーション実行 | |
app = QtGui.QApplication(sys.argv) | |
test = testView() | |
test.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment