こんにちは、Python でソフトウェアを作成しています。あまり得意ではありません。これまでのところ、プログラムでクラスを使用する必要はありませんでした。クラスを経由せずにマウスでイベントを実行したいのですが、コードはすべてクラスを使用しているため、プログラムを壊さずに使用することは困難です。
これが私のコードです:
def init_tree(win):
tw = QTreeWidget(win)
tw.resize(500, 500)
tw.setHeaderLabels(['TAGS'])
tw.setAlternatingRowColors(True)
tw = clear_tree(tw)
fill_tree(tw)
tw.show()
def main():
app = QApplication.instance()
if not app:
app = QApplication(argv)
window = widgets()
init_tree(window)
mouse_press_event(window)
exit(app.exec_())
main()
そして、ここに私が追加したいコードがあります
def contextMenuEvent(self, event):
contextMenu = QMenu(self)
newAct = contextMenu.addAction("New")
openAct = contextMenu.addAction("Open")
quitAct = contextMenu.addAction("Quit")
action = contextMenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAct:
self.close()
また
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
def mousePressEvent(self, QMouseEvent):
if QMouseEvent.button() == Qt.LeftButton:
print("Left Button Clicked")
elif QMouseEvent.button() == Qt.RightButton:
#do what you want here
print("Right Button Clicked")
自分の場合、パラメーターとして win を渡します
私が問題を抱えているのは、「event / QMouseEvent」パラメーターです