PDA

View Full Version : editorEvent parent question



lukeQt
22nd June 2015, 19:46
Hi Everyone,

I have a question with the editorEvent method. From my understanding this allows the QPushButton in the delegate to do something on the pushbutton event. This allows one to embed pushbuttons in a model and have the button do something. I am using a QtGui.QStyledItemDelegate and a QtCore.QAbstractItemModel. The goal is to make a tree of buttons and then have each button call a different script.

How do you pass the self in the class though? I want the new window to be a child of the main dialog, but this is in the delegate class not the gui class. Self would be the delegate not the main window gui.

How do you pass the parent widget in the delegate?


def editorEvent(self, event, model, option, index):
if event.type() == QtCore.QEvent.MouseButtonPress:
if index.data() == "tool 2":
self._o_grittool2 = tool2.Tool2(database=self._dbfname,
plan_table="plan", cfr_table="cfr", parent=self)
self._o_grittool2.exec_()
tool_executed = True

anda_skoa
22nd June 2015, 21:57
You can pass the widget you want to be used as a parent as an additional constructor argument to the delegate.
Or provide a setter method if the parent changes during the delegates life time.

Cheers,
_

lukeQt
23rd June 2015, 14:21
I see so I could pass the dialog in the delegate. What is a setter method?

I did it with a signal and slot which seem to be working nicely.





def editorEvent(self, event, model, option, index):
"""
When editing of an item starts, this function is called with the event
that triggered the editing, the model, the index of the item,
and the option used for rendering the item.

Mouse events are sent to editorEvent() even if they don't start editing
of the item. This can, for instance, be useful if you wish to open a
context menu when the right mouse button is pressed on an item.

The base implementation returns false (indicating that it has not
handled the event).

This is needed so when the createEditor is clicked then this is called.
This actually opens the import spatial data dialog.
"""
if event.type() == QtCore.QEvent.MouseButtonPress:
self.button_signal.emit(index.row(), index.column())
return True
elif event.type() == QtCore.QEvent.MouseButtonRelease:
return True

else:
return super(IssueDelegate, self).editorEvent(event, model, option, index)

# signal
self.delegate.button_signal.connect(self.importGIS )

def importGIS(self, val, val2):
## Instantiate the class.
self.file_browser = importGIS.ImportGIS(db,
title="Issues", fcType_filter=["Polyline", "Point", "Polygon"], sel_count="one", parent = self)

index = self.model.index(val, val2)
# Execute the file dialog.
# This will return the dialog to the user as a modal dialog.
if self.file_browser.exec_():
if self.file_browser.file_list_new > 0:

# Setdata method.
self.model.setData(index, os.path.basename(self.file_browser.file_list_new[0]))

anda_skoa
23rd June 2015, 15:13
I see so I could pass the dialog in the delegate. What is a setter method?

A "setter" is a method that changes the data of a specific field of the object.
Usually in pair with a "getter", a method that returns the value of a field in the object.

Something like


def setDialogParent(self, dialogParent):
self.dialogParent = dialogParent


Cheers,
_

lukeQt
23rd June 2015, 22:33
Is using a signal slot a good approach to achieving what I want? It seems to be working.

anda_skoa
24th June 2015, 08:50
Yes, that looks fine as well.

Cheers,
_

lukeQt
24th June 2015, 16:49
Thank you anda_skoa!!