Call MainWindow function inside Dialog



  • Hello, I have this code:

    #include "settings.h"
    #include "ui_settings.h"
    
    #include "mainwindow.h"
    
    #include <QApplication>
    
    settings::settings(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::settings)
    {
        ui->setupUi(this);
    }
    
    settings::~settings()
    {
        delete ui;
    }
    
    void settings::on_pushButton_clicked()
    {
        MainWindow::awesome_function();
        this->close();
    }
    

    Error:

    cannot call member function without object
    

    I know what does this error mean, it means, that I didn't create a MainWindow object, but problem is that if I create a MainWindow object, it will be a local variable (object) inside on_pushButton_clicked function, but I need to call that function from parent MainWindow, so I need to pass a MainWindow pointer to Dialog?

    Please any example how? :/

    Thank you


  • Moderators

    @t0msk

    With your call you are assuming that the called member function is static.

    BTW this is not a Qt issue. This is standard C++. You may want to read a bit in C++ tutorials


  • Qt Champions 2016

    @t0msk said

    • but I need to call that function from parent MainWindow, so I need to pass a MainWindow pointer to Dialog?

    MainWindow::awesome_function();

    This is means that awesome_function() should be static.
    A static member function can be called with no instance of the class and
    it cannot use any variables from the class.

    The difference is this
    MyClass classinst; << instance of class
    classinst.funcA();

    versus
    MyClass::funcA(); // no instance at all

    • so I need to pass a MainWindow pointer to Dialog?
      That would be a way, but its not optimal.
      You should emit a signal from Dialog and connect mainwindow and dialog
      so emitting the signal in Dialog will call the method in mainwindow.

    see this sample
    The dialog emits signal to make mainwindow switch a tab index.
    https://www.dropbox.com/s/lev1yukhpkgxhbe/myotherdialog.zip?dl=0

    Same method can be used to activate what ever you want from
    the dialog. Just change name and parameters of the slot and signal to suit
    your use case.



  • @mrjj
    Thanks both method works, but what is better to use and why? Signals or static?


  • Qt Champions 2016

    @t0msk
    Signals & slots!
    By far, far , far ! :)
    Static are not for calling a function in another class.
    It is for completely other design and should not be misused.
    Also, a static function is NOT allowed to use any data member in the class. ( with a few exceptions)

    So forget about static as it is not intended for your use case.

    Also, signals and slots give a great benefit that Dialog dont know anything about mainwindow.
    It simply emit a signal and how and what handle it, its not known to it.

    This means if you change something in mainwindow, you dont have to change qdialog also.

    For a larger program, such isolation is critical, as otherwise you get spagetti code
    and when you change something
    in one place, something stops working in other place.



  • @t0msk ,

    emit the signal from On_pushbutton_Clicked slot, call the mainwindow function in the slot



  • @mrjj
    Great, thank you very much :)


Log in to reply
 

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