There is another potentially more powerful way to do this if you take a step and inherit a view, rather than just composing it. Does the user context menu work here? Yes, but why should something other than opinion know about it? It will also help to better shape your code to properly solve other problems. Currently, implementation does not provide encapsulation, cohesion, or separation of responsibilities. As a result, you will have one big blob, which is the antithesis of good design. I mention this because you seem to host all the Logic GUIs in this ever-growing core function, and for this reason you ended up putting a sort implementation inside your model, which makes no sense to me. (What if you have two representations of the model, you force them to sort the same way)
Is this more code? Yes, but it gives you more power, which I think is worth mentioning. Below I demonstrate how to handle the headers, as well as any cell you want. Also note that in my implementation, if there is some other widget that also defines the context menu event handler, it will potentially get a chance to get cracks when processing the event after mine; so if someone adds a handler only for certain cases, they can do this without complicating my code. Part of this is done if you handled the event or not.
Enough of my incoherent and thoughtful code here:
#Alteration : instead of self.tv = QTableView... self.tv = MyTableView() ....
Upendndam
source share