[SOLVED]Access parent functions from class?

  • 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) :

    as one would expect.

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

  • Moderators

    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

  • 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

  • Moderators

    [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->setProperty("propertyName", value);

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

    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.

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

    Thank you.

  • 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?

  • Lifetime Qt Champion


    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.

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

    Congrats on being Qt 2014 Champion.

  • Lifetime Qt Champion

    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)


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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.