Unsolved Is it possible to override functions of widgets drawn in QT designer?
-
Edit:
Found out that I can actually overridemousePressEvent()
forQMainWindow
. I must have missed it when examing the document.Hi experts,
I'm trying to call a function when the mouse clicks a QGraphicsView/QGraphicsScene (AFAIK they are bounded brothers).First try is to call a
on_MyView_clicked()
which didn't work. After a bit of search I found thatQGraphicsView
doesn't allow this kind of thing (but I can do it with a button). I researched further and found a lot of posts here and on Stackoverflow saying that one should subclassQGraphicsScene
and overridemousePressEvent
from there.However I don't know how to do that as I drawed the widget in QT designer so it is not generated by code.
Question: Is there a way to do it without dynamically generating a
QGraphicsView
with a subclassedQGraphicsScene
? I think I can also subclassQMainWindow
and override from there, but I do prefer not to subclass anything at the moment.Thank you!
-
Hi
well to override events handlers for a class, there is no way around subclassing.
One can use an event filter to catch events but generally
subclassing is the most flexible way.- However I don't know how to do that as I drawed the widget in QT designer so it is not generated by code.
It's possible to use QCreators promote feature.
That allows you to make the subclass and then promote a normal the widget (in Designer) to your new type.
So you dont have to stop using your UI file and can also easy use subclassed widgets.https://doc.qt.io/qt-5/designer-using-custom-widgets.html
its not complicated to use. you just give it the class name of your new subclassed widget and the where to find it. (the .h files it lives in)