Call method in "parent" window from child window.



  • Hello Qt devs!

    I'm working on a Qt application which have multiple windows. With this I mean that from my main/parent window I open a new child window. From here I make some changes to a value, which also should be updated in my main/parent window, so when I close my child window, the value is already updated in the parent window. My question is then: How do I make this method call?

    I have tried using signals and slots:

    @connect(ui.button, SIGNAL(clicked()), this->parent(),
    SLOT(updateValue()));@

    The updateValue() method is placed in my parent window class.

    When I say parent and child, it is not something I have set up in any way. I only create a new instance of my child window from my parent. is this the right way to do it?

    Thank you for your time!



  • You will have to explicitly set your parent widget as a parent for your child widget, by either passing it as parent to the constructor or using QWidget::setParent().



  • Connect the child from within the parent is also possible. When some thing to be done, child emits a signal and parent catches and doing some work!

    How exactly are you creating a new window ? Using a QDialog or QWidget with out parent ?



  • My windows are sublcasses of QWidget.



  • How about:

    You give your child window a signal valueChanged(int theValue);

    You give your parent window a slot (either public or private, whatever you want) changeValue(int)

    At the place where you create the child window (that is: in the parent window), you create a signal-slot connection between the above two.

    In your child window, make sure you emit the valueChanged(int) signal whenever you need.

    Do not try to make the connection directly between a widget on a child window or widget and some internal slot. It is very bad design. Instead, make sure that each part of your application has a good API, and use that.



  • Thank you for your replies!

    I will try your suggestion Andre.



  • I'm not sure if I'm doing this correct?

    @Setup_selectMachine *selectMachine = new Setup_selectMachine();
    QObject::connect(selectMachine, SIGNAL(valueChanged()), this, SLOT(updateValue()));
    selectMachine->show();@

    The above code is where I declare, initialize and show my child window. I've placed the signal-slot connection there as well. "selectMachine" is the name of my child object. "valueChanged()" is the signal in "selectMachine" which should trigger the change. "this" of course refers to this class. "updateValue()" is the slot in this class which I want to be triggered. I am not sending any values directly via the connection, but instead I'm using a resource class which holds all values.

    So - Am I doing it the right way?

    Thank you for your time!



  • Well, looks like it is :).. Why dont you give it a try ?



  • Looks like a good start, if you actually created that signal and that slot.



  • Hm - Nothing seems to happen I'm afraid.

    I should mention that my slot method is completely empty. I just use it as a trigger.



  • A slot is not a trigger. A signal is (sort of). The slot should actually do something.



  • Oh my mistake. I meant signal. My signal is only a trigger :) Sorry



  • You do know that you are only supposed to give a declaration of a signal, right? No implementation allowed!

    So, only do:
    @
    //MyChildWindow.h
    class MyChildWindow: public QDialog
    {
    Q_OBJECT
    /...
    signals:
    void valueChanged(int);
    @

    Moc will provide an implementation for you.



  • Thank you!

    2 things:

    1. I did make an empty implementation. Deleted it.
    2. I forgot to declare it a signal in my header file. ("signals:")

    I now seems to be working. The variable is not updated properly, but I guess that's due to an error somewhere else.

    Thank you so much for your time!



  • [quote author="maxmotor" date="1316523853"]I'm not sure if I'm doing this correct?

    @Setup_selectMachine *selectMachine = new Setup_selectMachine();
    QObject::connect(selectMachine, SIGNAL(valueChanged()), this, SLOT(updateValue()));
    selectMachine->show();@[/quote]

    You forgot to specify the signal and slot parameters types (int).
    @ @Setup_selectMachine *selectMachine = new Setup_selectMachine();
    QObject::connect(selectMachine, SIGNAL(valueChanged(int)), this, SLOT(updateValue(int)));
    selectMachine->show(); @



  • No I did not - to quote myself:

    [quote author="maxmotor" date="1316523853"]I am not sending any values directly via the connection, but instead I'm using a resource class which holds all values.![/quote]

    So I don't have to send any parameters with the signal.



  • Sry must have missed that part.


Log in to reply
 

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