Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to access UI elements from subclass.
Forum Updated to NodeBB v4.3 + New Features

How to access UI elements from subclass.

Scheduled Pinned Locked Moved Unsolved General and Desktop
ui objectsubclassing
7 Posts 5 Posters 2.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NuMs
    wrote on last edited by
    #1

    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.
    Thanks

    jsulmJ 1 Reply Last reply
    0
    • K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • N NuMs

        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.
        Thanks

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ 1 Reply Last reply
        3
        • jsulmJ jsulm

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

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

          mrjjM jsulmJ 2 Replies Last reply
          0
          • JonBJ JonB

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            5
            • JonBJ JonB

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              0
              • jsulmJ jsulm

                @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).

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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....!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved