Re: [PyQt] QItemDelegate createEditor - help with focusOut on custom widget

2011-08-24 Thread James Holstead
Below is a working example. You'll see the PathWidget looses focus
when a combo box is selected. How can I delegate focus back to the
patchwidget when the user clicks outside of the entire TableView?



from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
import datetime


class testView(QTableView):

def __init__(self,parent,data):
QTableView.__init__(self,parent)
self.setSizePolicy(QSizePolicy.MinimumExpanding,
QSizePolicy.MinimumExpanding)

model = testModel(data)
self.setModel(model)
self.setItemDelegate(testDelegate(self))
self.setFixedWidth(600)
self.setColumnWidth(2,300)
self.show()

def focusOutEvent(self, evt):
print repr(self) + ' lost focus';

class testModel(QAbstractTableModel):

def __init__(self,data):
QAbstractTableModel.__init__(self)
self.data = data

def focusOutEvent(self, evt):
print repr(self) + ' lost focus';

def flags(self,index):
return Qt.ItemFlags(QAbstractTableModel.flags(self,index)
|Qt.ItemIsEditable)

def rowCount(self, index=QModelIndex()):
return 1

def columnCount(self,index=QModelIndex()):
return len(self.data)

def data(self,index,role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self.data[index.column()]

def setData(self, index, value, role=Qt.EditRole):
self.data[index.column()] = value.toString()
return True

class testDelegate(QItemDelegate):

def createEditor(self,parent,option,index):
if index.column() != 2:
editor = QLineEdit(parent)
return editor
else:
editor = PathEditor(parent)
return editor

def setModelData(self, editor, model, index):
if index.column() != 2:
model.setData(index,QVariant(editor.text()))
else:
model.setData(index,QVariant(editor.data()))

class PathEditor(QWidget):
def __init__(self, parent=None):
print type(parent)
super(PathEditor, self).__init__(parent)
self.setFocusPolicy(Qt.StrongFocus)
self.setAutoFillBackground(True)
self._lm = QGridLayout()
self._lm.setMargin(0)
self._initGui()

def focusOutEvent(self, evt):
print repr(self) + ' lost focus';

def _initGui(self):
for x in range(3):
combo_box = QComboBox()
combo_box.addItem('Option 1','One')
combo_box.addItem('Option 2','Two')
combo_box.addItem('Option 3','Three')
combo_box.setCurrentIndex(-1)
self._lm.addWidget(combo_box, 0,x)

self.setLayout(self._lm)

def data(self):
values = []
for child in self.children():
if isinstance(child, QComboBox):
if child.currentIndex() != -1:

values.append(str((child.itemData(child.currentIndex())).toString()))
res = ,.join(values)
return res

if __name__ == '__main__':

app = QApplication(sys.argv)
data = ['One','Two','Three']
v = testView(None,data = data)
app.exec_()
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


[PyQt] QItemDelegate createEditor - help with focusOut on custom widget

2011-08-22 Thread James Holstead
Greetings,

OS X 10.5.8
PyQT 4.8.3

I created a widget PathWidget which has a QGridLayout that contains 3
QComboBox's. This widget is only displayed in a specific column of a
QTableView using the createEditor method override of the QItemDelegate. We
started experiencing that the application crashes if someone uses this
widget and subsequently presses a copy button which basically copies all the
model's data and re-inits the main form widget and all its
children(including this table and item delegate).

What I noticed was this PathWidget, after being initialized, would not loose
focus unless the user clicked another object in the same TableView object.
The delegated PathWidget would still exist for that column when the user
clicked the copy button, somewhere causing a seg fault. Other components the
ItemDelegate creates, such as QComboBox and QLineEdit, loose focus and are
gc'd when the copy button is clicked.

Do I need to redefine any of the focus events for the PathWidget, and/or
perhaps set a proxy focus or policy?

Thank you,

James
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt