Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Call a function from child

Call a function from child

Scheduled Pinned Locked Moved Unsolved C++ Gurus
3 Posts 3 Posters 1.1k Views 3 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.
  • M Offline
    M Offline
    MSDQuick
    wrote on last edited by
    #1

    Hello;
    I would like to call a function from a child form by pressing a button, so I put these codes in my Main window form:

    void MainWindow::Test()
    {
        qDebug()<<"Hello World...";
    }
    

    And I put these codes in my Child form:

    #include "mainwindow.h"
    
    void PicExpand::on_pushButton_clicked()
    {
        MainWindow* myMainwindow = qobject_cast<MainWindow*>(parent());
        if (myMainwindow)
        {
            myMainwindow->Test();
        }
    }
    

    But, it doesn't work !

    Thanks for helping.
    MSD.

    raven-worxR 1 Reply Last reply
    0
    • M MSDQuick

      Hello;
      I would like to call a function from a child form by pressing a button, so I put these codes in my Main window form:

      void MainWindow::Test()
      {
          qDebug()<<"Hello World...";
      }
      

      And I put these codes in my Child form:

      #include "mainwindow.h"
      
      void PicExpand::on_pushButton_clicked()
      {
          MainWindow* myMainwindow = qobject_cast<MainWindow*>(parent());
          if (myMainwindow)
          {
              myMainwindow->Test();
          }
      }
      

      But, it doesn't work !

      Thanks for helping.
      MSD.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @MSDQuick said in Call a function from child:

      But, it doesn't work !

      then probably the widget is not a parent of the MainWindow?!

      --- 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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @raven-worx, what you are planning to do is bad design more specifically tight coupling. Why should the child know anything about its parent and furthermore call a function of it ?

        If you want your MainWindow to react to something happening in the child then you should use signals and slots.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2

        • Login

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