Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is it possible to override functions of widgets drawn in QT designer?



  • Edit:
    Found out that I can actually override mousePressEvent() for QMainWindow. 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 that QGraphicsView 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 subclass QGraphicsScene and override mousePressEvent 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 subclassed QGraphicsScene? I think I can also subclass QMainWindow and override from there, but I do prefer not to subclass anything at the moment.

    Thank you!


  • Lifetime Qt Champion

    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)


Log in to reply