SOLVED: Call MainWindow function from Dialog



  • Hello,

    Ok I have been doing C++ and Qt for just 2 months. I can say I have mastered them quite well since I had prior programming experience. Except for a few nagging issues like, Am not able to call a public member function of the MainWindow class from inside the cpp file of a dialog class. The code compiles well but there is no response when I click the button connected.
    The following code is the private slot of a QPushButton clicked() signal but nothing happens.

    inside dialog.cpp
    @
    void Dialog::button_clicked()
    {
    MainWindow object;
    object.addition();
    }
    @

    Thanks in advance



  • Hi Qkato.

    In some case i use emit signal from dialog and capture this signal in mainwindow.
    I can't access to my code at the moment, but it's not difficult.
    Hope it's util
    Regards.



  • Could you please elaborate more on emit



  • [quote author="Qkato" date="1373983270"]Hello,

    The following code is the private slot of a QPushButton clicked() signal but nothing happens.

    inside dialog.cpp
    @
    void Dialog::button_clicked()
    {
    MainWindow object;
    object.addition();
    }
    @
    [/quote]

    Hi, when you button clicked, you create a local MainWindow object, call one public member of it, then destroy the MainWindow object.

    If you have added qDebug() or similiar statement to your MainWindow's constructor, addition, destructor functions, you should find that they all works as expected.



  • I understand that MainWindow is QMainWindow implementation, and it's parent of Dialog. You should use SergioDanielG way using signals, or pass MainWindow reference to Dialog and use it directly. However, it's little strange for me accessing whole MainWindow from child Dialog.

    This way or the other you should not create another instance of MainWindow as you shown in your example.


  • Moderators

    [quote author="Qkato" date="1373986476"]Could you please elaborate more on emit[/quote]Search for the word "emit" in the "Signals & Slots documentation":http://qt-project.org/doc/qt-5.1/qtcore/signalsandslots.html#a-small-example. Pay attention to the section called "A Small Example".

    [quote author="Qkato" date="1373983270"]
    @
    void Dialog::button_clicked()
    {
    MainWindow object;
    object.addition();
    }
    @[/quote]This creates a NEW MainWindow, and then calls addition() on your new MainWindow instance -- it doesn't interact with your old MainWindow.

    Furthermore, object is a local variable -- it gets destroyed when button_clicked() returns.



  • Hi,

    [quote author="Qkato" date="1373983270"]Ok I have been doing C++ and Qt for just 2 months.[/quote]

    If after 2 months you don't understand about Signal & Slots you MUST study more.

    As JKSH said you must also pay attention about local defined variables; IMHO you must study more also about C++


Log in to reply
 

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