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. [SOLVED]Access parent functions from class?
QtWS25 Last Chance

[SOLVED]Access parent functions from class?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 4.6k 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.
  • ealioneE Offline
    ealioneE Offline
    ealione
    wrote on last edited by
    #1

    I have a main window that uses various other classes.

    Inside that main window there is a menu with various submenus and I need to access it from one of my classes. What I have now is a call to my class from the main window like this

    @classA = new ClassA(this);@

    the constructor of ClassA is

    @ClassA:: ClassA(QObject *parent) :
    QObject(parent)
    {@

    as one would expect.

    How can I access inside this class the various public functions and variables of my main window?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      There are multiple ways to get this result:

      Pass main window pointer to ClassA

      Make main window a Singleton

      Pass only required information to ClassA (not the whole main window pointer, only the info it needs)

      Use signals and slots to notify different components that they should update themselves

      (Z(:^

      1 Reply Last reply
      0
      • ealioneE Offline
        ealioneE Offline
        ealione
        wrote on last edited by
        #3

        Oddly enough I can very well do 3 and 4 :) yet what I need to do is 1.

        I thought that the '*parent' in my ClassA constructor was the main window pointer...

        Could you possibly give an example

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          [quote author="ealione" date="1421933586"]I thought that the '*parent' in my ClassA constructor was the main window pointer...[/quote]

          It is. But if you begin depending on this and then decide to move ClassA to a different place, under a different parent (or create a second instance somewhere else) you will get runtime errors. So it is a bit risky. If you really want to, you can either work with parent using the Meta Object System, or cast the pointer:
          @
          // Meta object system:
          parent->invokeMethod("doSomething");
          parent->setProperty("propertyName", value);

          // Pointer casting:
          MyMainWindow *mainWindow = qobject_cast<MyMainWindow *>(parent);
          if (mainWindow != 0) {
          mainWindow->doSomething();
          }
          @

          You have asked for the solution for passing the main window pointer. It is very easy. Just create a methos (or a constructor) that takes in a pointer to your MainWindow, and use it inside your class.

          (Z(:^

          1 Reply Last reply
          0
          • ealioneE Offline
            ealioneE Offline
            ealione
            wrote on last edited by
            #5

            Got it, seems like pointer casting is the way to go.

            Thank you.

            1 Reply Last reply
            0
            • ealioneE Offline
              ealioneE Offline
              ealione
              wrote on last edited by
              #6

              Quick question,

              How can I make mainWindow global so I can use it in various functions in my class, or pass it as a parameter?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Following your thread, I'd recommend first re-thinking your application design.

                Having child widgets needing to know their parent is generally a sign of bad design since it introduces tight coupling (sometimes it might be needed but it's not the usual case). But making your MainWindow global sounds like you are trying to call things from it from several different places and you are trying to bypass good programming rules.

                You should first take a look at Qt's examples and demos as well as the signal and slot chapter. You can also describe what you would like to achieve here so we can better help you reach your goal.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • ealioneE Offline
                  ealioneE Offline
                  ealione
                  wrote on last edited by
                  #8

                  Well that ClassA I mentioned before is just going to add a menu entry on my main window and also add some basic scripting abilities, again to my main window.
                  So the way I imagined this is to use ClassA, pass it a pointer to main window and then use that pointer to find the menu bar, add my settings there and then create a script engine using that same pointer.

                  EDIT:
                  Congrats on being Qt 2014 Champion.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Then it should be the other way around. ClassA should expose its menu/toolbar whatever to the MainWindow and it's up to it to handle the connection etc.

                    Doing so you can easily swap QMainWindow for something else. You will also be able to centralize the management of the menus/toolbars in QMainWindow (e.g. regrouping menus)

                    Thanks

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • ealioneE Offline
                      ealioneE Offline
                      ealione
                      wrote on last edited by
                      #10

                      I see, seems like a good idea.
                      To be honest I was looking for some advise on how to efficiently add scripting abilities to my app, thats why I was looking at how "Amarok":http://quickgit.kde.org/?p=amarok.git implemented scripting, but Im afraid I didn't understood much from there so your advise is most welcome, thanks.

                      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