Reply
Thread Tools
Posts: 29 | Thanked: 4 times | Joined on Dec 2009 @ austria
#1
hey there,

I'm wrting a simple gui for a program using eric4, qtdesigner and pyqt4 on maemo. Part of that gui is a Qlistview (http://www.riverbankcomputing.co.uk/...qlistview.html).
Qlistview natively support click and doubleclick signals, allowing me to react on these user actions, ie:

Code:
clicked ( const QModelIndex & index )
This is working fine. However, what I need now is a second signal, different from a simple click, like a right click, to allow more complex user input. A right click on a touchscreen translates to a longer press on the item, right? But event does it translate to? Or how do I code such a custom event?
I have already added a custom signal to react on right clicks/contextMenus:

Code:
#custom right click event
self.listView.setContextMenuPolicy(Qt.CustomContextMenu) self.listView.connect(self.listView, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)
I do get the signal, however, I also get the usual click signal and therefore I cannot react accordingly. Additionally I do lose the index of the item in the list, which I definitely need.

How does one implement "right clicks" correctly without losing the index information and being able to discriminate between the two events?

greetings,
jinx
 
fpp's Avatar
Posts: 2,853 | Thanked: 968 times | Joined on Nov 2005
#2
This should translate to "ContextMenu". You need to set a property to enable it and decide how you want to handle it (action menu with default handler, custom slot with signal handler).
 

The Following User Says Thank You to fpp For This Useful Post:
Posts: 29 | Thanked: 4 times | Joined on Dec 2009 @ austria
#3
Hello fpp,

thanks for your answer. I'm not sure I can follow you. I do get the "contextMenu" signal and may also react on it. However, I still do get the *click* signal, since QlistView does not distinguish between left and right click.

how do other people achieve having a simple "touch event" and the "press for one second" event? is there a recommended way to do this?

greetings,
jinx
 
Posts: 29 | Thanked: 4 times | Joined on Dec 2009 @ austria
#4
hello again,

I think what I need is discrimnate between "tap" and "tap-and-hold". is this possible when dealing with a QListview?

greetings,
jinx
 
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#5
I have been able to achieve what I think you are looking for, though I am using a QListWidget, rather than a QListView.

To obtain a context menu following a long press, I first set the contextMenuPolicy:

Code:
self.listWidget.setContextMenuPolicy(Qt.ActionsContextMenu)
Then I add some QActions to the listWidget, e.g:

Code:
self.actionEdit = QAction("Edit", self.listWidget)
self.actionDelete = QAction("Delete", self.listWidget)
self.listWidget.addAction(self.actionEdit)
self.listWidget.addAction(self.actionDelete)
Now, when you long-press an item in the QListWidget/View, the context menu containing the two QActions will appear. You can obtain the currently selected row using the currentRow() method, e.g:

Code:
self.connect(self.actionEdit, SIGNAL("triggered()"), self.testMethod)

def testMethod(self):
row = self.listWidget.currentRow() print row
 
Posts: 29 | Thanked: 4 times | Joined on Dec 2009 @ austria
#6
hey marxian,

thanks for your answer. that's good to know, but actually not what I have been looking for. nevertheless I have figured it out in the meantime. For now it's nothing more than an ugly hack though

what I want/need is the following:
Code:
on left click on a list item - do action 1
on right click on a list item - do action 2 (no action menu, just do action 2)
so in a real world scenario:

Code:
on left click: play song
on right click: show id3 information
this would be no problem at all, if I there was (is there?) a method to determine the button clicked, because QListview's signal is triggered whenever you click any button, left or right.
luckily, when using the setContextMenu approach, the linked method is always triggered before the clicked signal. So I can set self.right = True in this method. when clicked is called afterwards it determines if self.right is set or not and acts accordingly.

this is the ugly solution I ended up with for now. I'm sure I can come up with something better, when I programmed some more QT/PyQT.

Code:
 #custom right click event
self.right = False
self.listView.setContextMenuPolicy(Qt.CustomContextMenu)
self.listView.connect(self.listView, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)
and self on right click does nothing more than setting the boolean:

Code:
def onRightClick(self):
        self.right = True
and then

Code:
def on_listView_clicked(self, index):
	 if self.right:
		action1	 
	 else:
		action2
greetings,
jinx
 
Reply


 
Forum Jump


All times are GMT. The time now is 08:55.