How to execute a MainWindow method from Dialog class



  • Hello, everyone. I hava a method in MainWindow class named ShowText() (for example) that writes a text in a line edit.

    void MainWindow::ShowText()
    {
    ui->leText->setText("I write this text");
    return;
    }
    and I want execute this method from a Dialog form to push a button.

    What code have I to write when the pushButton is clicked?

    void Dialog::on_pbSText_clicked()
    {
    // ¿ ....... ?
    }

    Thanks in advance!


  • Moderators

    easiest would probably be to connect a dialog signal (connected to the button's clciked signal) with the main window slot.


  • Moderators

    Yes, as raven-worx sad you should emit a signal from your dialog which you can connect to a slot in your MainWindow.
    The dialog should not know anything about MainWindow and what it does if a button is clicked in the dialog.



  • Hi, welcome to Devnet,

    As I explained in this similar post
    https://forum.qt.io/topic/61728/how-to-access-methods-of-mainwindow-from-dialog-form-subclassing-i-guess/3

    As a rule of thumb, I recommend you to make connections from the parent widget, who is aware of it's children since children are often created from parent widget:

    the main window creates the dialog;
    the dialog can emit a signal when its button is pushed;
    the main window connects this signal to a custom slot;
    the slot processes the dialog information.
    

    Moreover, creating connections from the parent widget enables you to make two children communicate between them, without having to know each other.



  • Well, I declared the method in "mainwindow.h" as public slots:
    public slots:
    void ShowText();

    Then, in the "dialog.cpp" constructor:
    connect(ui->pbSText,SIGNAL(clicked()),parent, SLOT(ShowText()));

    but it do nothing.
    When i push "pbSText" button i think the slot "ShowText()" was execute but nothing happend.

    What is wrong?.
    Thanks a lot.



  • No, the connect is in the MainWindow constructor:

    connect(myDialog, SIGNAL(clicked()), this, SLOT(ShowText()));
    

    If you want your dialog to emit a signal when its ui->pbSText emits the clicked signal, then declare a clicked signal in dialog.h:

    signals:
      void myClicked();
    

    Then connect it from your constructor in dialog.cpp:

    connect(ui->pbSText,SIGNAL(clicked()), this, SIGNAL(myClicked()));
    

    To check if a SLOT is really called, just output some stuff, using qDebug(), or std::cout, or anything you like to print messages.



  • GOOD, it works fine.

    But the connect in MainWindow is:
    connect(myDialog, SIGNAL(myClicked()), this, SLOT(ShowText()));

    Thanks a lot.
    I'm very happy.



  • @CarlosQ said:

    But the connect in MainWindow is:
    connect(myDialog, SIGNAL(myClicked()), this, SLOT(ShowText()));

    Exaxctly, that means you understood what I was talking about =)

    Thanks a lot.
    I'm very happy.

    Glad to hear that!


Log in to reply
 

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