Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to execute a MainWindow method from Dialog class

How to execute a MainWindow method from Dialog class

Scheduled Pinned Locked Moved Solved General and Desktop
method
8 Posts 4 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CarlosQ
    wrote on 15 Jan 2016, 11:00 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 15 Jan 2016, 11:02 last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 15 Jan 2016, 12:10 last edited by
        #3

        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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • V Offline
          V Offline
          ValentinMichelet
          wrote on 15 Jan 2016, 20:11 last edited by ValentinMichelet
          #4

          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.

          1 Reply Last reply
          1
          • C Offline
            C Offline
            CarlosQ
            wrote on 16 Jan 2016, 11:30 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              ValentinMichelet
              wrote on 16 Jan 2016, 11:55 last edited by ValentinMichelet
              #6

              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.

              1 Reply Last reply
              2
              • C Offline
                C Offline
                CarlosQ
                wrote on 16 Jan 2016, 12:27 last edited by
                #7

                GOOD, it works fine.

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

                Thanks a lot.
                I'm very happy.

                V 1 Reply Last reply 16 Jan 2016, 12:34
                1
                • C CarlosQ
                  16 Jan 2016, 12:27

                  GOOD, it works fine.

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

                  Thanks a lot.
                  I'm very happy.

                  V Offline
                  V Offline
                  ValentinMichelet
                  wrote on 16 Jan 2016, 12:34 last edited by ValentinMichelet
                  #8

                  @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!

                  1 Reply Last reply
                  0

                  8/8

                  16 Jan 2016, 12:34

                  • Login

                  • Login or register to search.
                  8 out of 8
                  • First post
                    8/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved