Hello,

I'm trying to create a context menu on right click event and it seems to me
that when the code is run on the N900, the context menu is always shows up
on the widget BELOW the one that I actually right-clicked on. The same code,
however, runs fine on Windows.
Here it is:

---

import sys
from PyQt4.Qt import QApplication, QMainWindow, QWidget, \
                     QVBoxLayout, QListView, QAbstractListModel, \
                     Qt, QVariant, QAbstractListModel, \
                     QEvent, QMenu, QAction,QLabel

class RCListModel(QAbstractListModel):

    def __init__(self):
        QAbstractListModel.__init__(self)
        self.internalList = ["Line 1",
                             "Line 2",
                             "Line 3",
                             "Line 4",
                             "Line 5"]

    def rowCount(self, parent):
        return len(self.internalList)

    def data(self, index, role = Qt.DisplayRole):

        if role == Qt.DisplayRole:
            return self.internalList[index.row()]

        return QVariant()


class RCListView(QListView):

    def __init__(self, parent=None):
        QListView.__init__(self, parent)
        self.viewport().installEventFilter(self)

    def eventFilter(self, obj, event):
        '''
        @param obj QObject
        @paramn event QEvent
        '''
        if (event.type() == QEvent.ContextMenu):

            print event.globalPos()

            # event is a QMouseEvent
            menu = QMenu(self)

            menu.addAction(QAction("Context Menu 1",self))
            menu.addAction(QAction("Context Menu 2",self))
            pos = self.viewport().mapToGlobal(event.pos())
            menu.exec_(pos)

        return QListView.eventFilter(self, obj, event)


class RCMainWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        widget = QWidget(self)
        self.setWindowTitle("Right click demo")
        self.setCentralWidget(widget)
        self.vbox = QVBoxLayout(widget)

        self.model = RCListModel()
        self.listView = RCListView(self)
        self.listView.setModel(self.model)
        self.vbox.addWidget(QLabel("My list"))
        self.vbox.addWidget(self.listView)

class MinimalRightClick:
    def __init__(self):
        self.ui = RCMainWindow()
        self.ui.show()

if __name__ == '__main__':
    qtApp = QApplication(sys.argv)
    qtApp.setProperty("FingerScrollBars", False)
    app = MinimalRightClick()
    sys.exit(qtApp.exec_())

---
Any thoughts?

Thanks,
Shing
_______________________________________________
maemo-developers mailing list
maemo-developers@maemo.org
https://lists.maemo.org/mailman/listinfo/maemo-developers

Reply via email to