Qt World Summit: Submit your Presentation

How to access UI elements from subclass.

  • Hello!

    New to QT.

    My current project has one GBUI and I need to be able to access elements of the UI from subclasses. None of the subclasses have a UI.

    How do I pass Ui::MainWindow to subclasses?

    I've tried to pass it through the constructor like below:

    SubClass::SubClass(Ui::MainWindow mw) {
    Ui::MainWindow mw = mw;

    Which of course doesn't work.

    Any help would be appreciated.

  • If you must do it that way why not just pass the pointer of your main window?
    Then you could make the ui pointer available through a getter, or just make it public.

  • Lifetime Qt Champion

    @NuMs If this subclasses are not UI classes why should they have direct access to the UI? It sounds like bad software design. You should rather use signals/slots to communicate between these classes and UI classes.

  • @jsulm
    Just OOI, if the sub-classes have "no UI", how would they even do slots/signals between themselves and UI layer?

  • Lifetime Qt Champion

    They just need to be QObjects and have the Q_OBJECT macro to send/get signals.

  • Lifetime Qt Champion

    @JNBarchan "how would they even do slots/signals between themselves and UI layer?" - not sure I understand the question. Signals/Slots can be used between any type of classes, the only requirement is: they need to be derived from QObject (QObject isn't a UI class).

  • @jsulm
    TBH, I'm not I exactly sure I understand my own question!

    @mrjj & you explained the practicalities --- you just need a QObject. (I am PyQt, so I don't have a "Q_OBJECT macro", but I guess PyQt's QObject() automatically has that present if I use such an object or inherit from it?)

    My question was more of a "conceptual" one. If the OP has subclasses "with no UI", they are divorced from the UI, so I don't really see how they & the UI would know which signals to exchange/place slots on. But it's a bit of a woolly question....!

Log in to reply