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.

How to access UI elements from subclass.

Scheduled Pinned Locked Moved Unsolved General and Desktop
ui objectsubclassing
7 Posts 5 Posters 2.3k Views
  • 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 1 Dec 2017, 05:46 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

    J 1 Reply Last reply 1 Dec 2017, 13:09
    0
    • K Offline
      K Offline
      kenchan
      wrote on 1 Dec 2017, 12:23 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
        1 Dec 2017, 05:46

        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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Dec 2017, 13:09 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

        J 1 Reply Last reply 1 Dec 2017, 19:31
        3
        • J jsulm
          1 Dec 2017, 13:09

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

          J Offline
          J Offline
          JonB
          wrote on 1 Dec 2017, 19:31 last edited by JonB 12 Jan 2017, 19:32
          #4

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

          M J 2 Replies Last reply 1 Dec 2017, 19:40
          0
          • J JonB
            1 Dec 2017, 19:31

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 1 Dec 2017, 19:40 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
            • J JonB
              1 Dec 2017, 19:31

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 4 Dec 2017, 06:19 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

              J 1 Reply Last reply 4 Dec 2017, 08:51
              0
              • J jsulm
                4 Dec 2017, 06:19

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

                J Offline
                J Offline
                JonB
                wrote on 4 Dec 2017, 08:51 last edited by JonB 12 Apr 2017, 08:51
                #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

                3/7

                1 Dec 2017, 13:09

                • Login

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