Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Maemo: Developers

PyQt4 mapToGlobal

 

 

Maemo developers RSS feed   Index | Next | Previous | View Threaded


shing.n900 at gmail

May 6, 2010, 3:59 AM

Post #1 of 1 (286 views)
Permalink
PyQt4 mapToGlobal

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 RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.