From 112adb3332c5e52a90ad302430f28c15c0542fc1 Mon Sep 17 00:00:00 2001 From: "Christopher T. Johnson" Date: Tue, 12 Mar 2024 11:25:45 -0400 Subject: [PATCH] Add Context Menu to the people view Fixes: #1 added contextMenuEvent handler. Make sure that we only present the menu when the cursor is over a name. --- main.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 25823c5..2eda037 100755 --- a/main.py +++ b/main.py @@ -20,7 +20,8 @@ from PyQt6.QtCore import ( pyqtSlot, ) from PyQt6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel -from PyQt6.QtWidgets import QApplication, QFileDialog, QMainWindow +from PyQt6.QtWidgets import QApplication, QFileDialog, QMainWindow, QMenu +from PyQt6.QtGui import QContextMenuEvent from lib import Book, PersonDialog, ReadDialog, SessionDialog, query_error from lib.preferences import Preferences @@ -59,6 +60,20 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.show() return + def contextMenuEvent(self, event: QContextMenuEvent) -> None: + localPos = self.peopleView.mapFromGlobal(event.globalPos()) + if not self.peopleView.rect().contains(localPos): + return + index = self.peopleView.indexAt(localPos) + if index.row() < 0: + return + menu = QMenu(self) + menu.addAction(self.actionRead) + self.actionEditPerson.setEnabled(True) + menu.addAction(self.actionEditPerson) + menu.exec(event.globalPos()) + return + @pyqtSlot() def editPreferences(self) -> None: dlg = Preferences()