Add double click and require people have books assigned before double click

This commit is contained in:
Christopher T. Johnson
2023-11-15 21:56:13 -05:00
parent 2b2f461d2f
commit a6cacdb538

23
main.py
View File

@@ -4,7 +4,7 @@ import re
import sys
from typing import cast
from PyQt6.QtCore import Qt, pyqtSlot
from PyQt6.QtCore import QModelIndex, Qt, pyqtSlot
from PyQt6.QtGui import (
QAction,
QFont,
@@ -19,8 +19,6 @@ from PyQt6.QtWidgets import QApplication, QFileDialog, QMainWindow
from lib import *
from ui.MainWindow import Ui_MainWindow
# from ui.testWindow import Ui_MainWindow
def query_error(query: QSqlQuery) -> None:
print(
@@ -34,17 +32,28 @@ def query_error(query: QSqlQuery) -> None:
raise Exception("SQL Error")
class ModelOverride(QSqlQueryModel):
def flags(self, index: QModelIndex) -> Qt.ItemFlag:
itemFlags = super(ModelOverride, self).flags(index)
value = index.siblingAtColumn(3).data()
if not value or value < 1:
itemFlags &= ~(Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled)
return itemFlags
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self) -> None:
super(MainWindow, self).__init__()
self.setupUi(self)
model = QSqlQueryModel()
# model = QSqlQueryModel()
model = ModelOverride()
query = QSqlQuery("SELECT * FROM people ORDER BY name")
model.setQuery(query)
self.peopleView.setModel(model)
self.peopleView.setModelColumn(1)
self.ReadButton.clicked.connect(self.readAction)
self.bookBtn.clicked.connect(self.bookAction)
self.peopleView.doubleClicked.connect(self.readAction)
self.createActions()
self.show()
return
@@ -148,7 +157,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
return
@pyqtSlot()
def readAction(self) -> None:
@pyqtSlot(QModelIndex)
def readAction(self, index: QModelIndex | None = None) -> None:
if index:
person_id = index.siblingAtColumn(0).data()
else:
indexes = self.peopleView.selectedIndexes()
if len(indexes) < 1:
return