捕捉在项目上按下了哪个鼠标按钮 [英] Catch which mousebutton is pressed on item

查看:103
本文介绍了捕捉在项目上按下了哪个鼠标按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有mainWindow类定义的UI.py文件(使用Qt Designer和Pyside构建). UI被导入到主模块中. 在UI中,我有一个列表框.基于鼠标左键或右键单击某个项目,必须执行一个过程,例如leftMouseClicked和rightMouseClicked. 感谢您的帮助.

I have a UI.py file with the mainWindow class definition (build with Qt Designer and Pyside). The UI is imported in the main module. In the Ui I have a listbox. Based on the left or right mouse clicked on an item a procedure must be executed, e.g leftMouseClicked and rightMouseClicked. Thanks for your help.

推荐答案

,您可以在QListWidget子类中定义mousePressEvent()方法来处理鼠标按下事件.获取事件参数单击哪个按钮并将其保存到属性.

you can define a mousePressEvent() method in your QListWidget subclass to handle mouse press event. Get which button is clicked by the event parameter and save it to an attribute.

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyListWidget(QListWidget):
    def __init__(self, parent=None):
        super(MyListWidget, self).__init__(parent)
        self.itemClicked.connect(self.on_item_clicked)

    def mousePressEvent(self, event):
        self._mouse_button = event.button()
        super(MyListWidget, self).mousePressEvent(event)

    def on_item_clicked(self, item):
        print item.text(), self._mouse_button

class Frame(QWidget):
    def __init__(self, parent=None):
        super(Frame, self).__init__(parent)    
        self.item_ctrl = items = MyListWidget(self)     
        self.item_ctrl.addItem("Item1")
        self.item_ctrl.addItem("Item2")
        box = QVBoxLayout()
        box.addWidget(self.item_ctrl)
        self.setLayout(box)

if __name__ == "__main__":
    import sys    
    app = QApplication(sys.argv)
    main = Frame()
    main.show()
    sys.exit(app.exec_())

如果使用QListView:

If you use QListView:

import sys
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

def main(): 
    app = QApplication(sys.argv) 
    w = MyWindow() 
    w.show() 
    sys.exit(app.exec_()) 

class MyWindow(QWidget): 
    def __init__(self, *args): 
        QWidget.__init__(self, *args) 

        # create table
        list_data = [1,2,3,4]
        lm = MyListModel(list_data, self)
        lv = MyListView()
        lv.setModel(lm)
        lv.clicked.connect(self.item_clicked)
        self.lv = lv

        # layout
        layout = QVBoxLayout()
        layout.addWidget(lv) 
        self.setLayout(layout)

    def item_clicked(self, index):
        print "row=", index.row(), "button=", self.lv._mouse_button

class MyListView(QListView):
    def mousePressEvent(self, event):
        self._mouse_button = event.button()
        super(MyListView, self).mousePressEvent(event)

class MyListModel(QAbstractListModel): 
    def __init__(self, datain, parent=None, *args): 
        """ datain: a list where each item is a row
        """
        QAbstractListModel.__init__(self, parent, *args) 
        self.listdata = datain

    def rowCount(self, parent=QModelIndex()): 
        return len(self.listdata) 

    def data(self, index, role): 
        if index.isValid() and role == Qt.DisplayRole:
            return QVariant(self.listdata[index.row()])
        else: 
            return QVariant()

if __name__ == "__main__": 
    main()

这篇关于捕捉在项目上按下了哪个鼠标按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆