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. Call MainWindow function inside Dialog
Forum Updated to NodeBB v4.3 + New Features

Call MainWindow function inside Dialog

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 3.6k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    t0msk
    wrote on 10 Sept 2017, 10:39 last edited by t0msk 9 Oct 2017, 10:40
    #1

    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

    Student who loves C/C++

    K 1 Reply Last reply 10 Sept 2017, 11:09
    0
    • T t0msk
      10 Sept 2017, 10:39

      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

      K Offline
      K Offline
      koahnig
      wrote on 10 Sept 2017, 11:09 last edited by
      #2

      @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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 10 Sept 2017, 11:23 last edited by
        #3

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

        T 1 Reply Last reply 10 Sept 2017, 12:19
        1
        • M mrjj
          10 Sept 2017, 11:23

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

          T Offline
          T Offline
          t0msk
          wrote on 10 Sept 2017, 12:19 last edited by
          #4

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

          Student who loves C/C++

          M 1 Reply Last reply 10 Sept 2017, 16:11
          1
          • T t0msk
            10 Sept 2017, 12:19

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 10 Sept 2017, 16:11 last edited by mrjj 9 Oct 2017, 16:18
            #5

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

            T 1 Reply Last reply 10 Sept 2017, 16:51
            1
            • Vinod KuntojiV Offline
              Vinod KuntojiV Offline
              Vinod Kuntoji
              wrote on 10 Sept 2017, 16:20 last edited by
              #6

              @t0msk ,

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

              C++, Qt, Qt Quick Developer,
              PthinkS, Bangalore

              1 Reply Last reply
              1
              • M mrjj
                10 Sept 2017, 16:11

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

                T Offline
                T Offline
                t0msk
                wrote on 10 Sept 2017, 16:51 last edited by
                #7

                @mrjj
                Great, thank you very much :)

                Student who loves C/C++

                1 Reply Last reply
                1

                1/7

                10 Sept 2017, 10:39

                • Login

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