PDA

View Full Version : QWidgetAction: A QTreeWidget in a QMenu



chezifresh
13th October 2009, 00:51
I'm trying to get a QTreeWidget into the submenu of a context menu. (This is to allow users to selected alternate versions of the thing they're right clicking on).

I created a custom QWidgetAction but for some reason the widget doesnt disappear when the action is triggered. Any ideas on why this isnt working??



class QdVersionListAction(QtGui.QWidgetAction):
def __init__(self, path, parent=None):
QtGui.QWidgetAction.__init__(self, parent)
self.path = path
self.new_version = ""

def createWidget(self, parent):
widget = QdVersionTree(self.path, parent)
widget.setMinimumSize(450, 100)
self.connect(widget, SIGNAL("versionSelected(QString)"), self.versionAccepted)
self.activate(QtGui.QAction.Trigger)
return widget

def versionAccepted(self, path):
self.new_version = path
# this works, the triggered signal is emitted and all
self.activate(QtGui.QAction.Trigger)

# What else can I do here????
self.emit(SIGNAL("changed()"))
self.toggle()
print 'version accepted', path

class QdVersionTree(QtGui.QTreeWidget):
def __init__(self, path, parent=None):
QtGui.QTreeWidget.__init__(self, parent)
self.setAllColumnsShowFocus(True)

attributes = ["version", "name", "creator", "description"]
self.setHeaderLabels(atributes)
self.setFrameStyle(QtGui.QFrame.NoFrame)

versions = [1, 2, 3, 4, 5, 6]
for version in versions:
columns = []
for attribute in attributes:
columns.append(attribute)

item = QdVersionTreeItem(self, columns)
self.addTopLevelItem(item)

self.connect(self, SIGNAL("itemActivated(QTreeWidgetItem *, int)"),
self.itemActivated)

def itemActivated(self, item, column):
self.emit(SIGNAL("versionSelected(QString)"), "foo")

chezifresh
16th October 2009, 04:49
I found this answer but it doesnt seem to work for a QTreeView/QTreeWidget... any ideas?

http://lists.trolltech.com/qt-interest/2007-06/thread00463-0.html


void MyButton::mouseReleaseEvent(QMouseEvent *event)
{
// this one triggers the action and untoggles the button
QToolButton::mouseReleaseEvent(event);
// this one forwards the event to the parent
QWidget::mouseReleaseEvent(event);
}