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

SOLVED: Call MainWindow function from Dialog

Scheduled Pinned Locked Moved General and Desktop
7 Posts 6 Posters 5.3k Views 1 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.
  • Q Offline
    Q Offline
    Qatto
    wrote on last edited by
    #1

    Hello,

    Ok I have been doing C++ and Qt for just 2 months. I can say I have mastered them quite well since I had prior programming experience. Except for a few nagging issues like, Am not able to call a public member function of the MainWindow class from inside the cpp file of a dialog class. The code compiles well but there is no response when I click the button connected.
    The following code is the private slot of a QPushButton clicked() signal but nothing happens.

    inside dialog.cpp
    @
    void Dialog::button_clicked()
    {
    MainWindow object;
    object.addition();
    }
    @

    Thanks in advance

    Web/Desktop Developer

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SergioDanielG
      wrote on last edited by
      #2

      Hi Qkato.

      In some case i use emit signal from dialog and capture this signal in mainwindow.
      I can't access to my code at the moment, but it's not difficult.
      Hope it's util
      Regards.

      www.ftatv.com.ar El foro argentino de la TV libre

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qatto
        wrote on last edited by
        #3

        Could you please elaborate more on emit

        Web/Desktop Developer

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          [quote author="Qkato" date="1373983270"]Hello,

          The following code is the private slot of a QPushButton clicked() signal but nothing happens.

          inside dialog.cpp
          @
          void Dialog::button_clicked()
          {
          MainWindow object;
          object.addition();
          }
          @
          [/quote]

          Hi, when you button clicked, you create a local MainWindow object, call one public member of it, then destroy the MainWindow object.

          If you have added qDebug() or similiar statement to your MainWindow's constructor, addition, destructor functions, you should find that they all works as expected.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bodzio131
            wrote on last edited by
            #5

            I understand that MainWindow is QMainWindow implementation, and it's parent of Dialog. You should use SergioDanielG way using signals, or pass MainWindow reference to Dialog and use it directly. However, it's little strange for me accessing whole MainWindow from child Dialog.

            This way or the other you should not create another instance of MainWindow as you shown in your example.

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              [quote author="Qkato" date="1373986476"]Could you please elaborate more on emit[/quote]Search for the word "emit" in the "Signals & Slots documentation":http://qt-project.org/doc/qt-5.1/qtcore/signalsandslots.html#a-small-example. Pay attention to the section called "A Small Example".

              [quote author="Qkato" date="1373983270"]
              @
              void Dialog::button_clicked()
              {
              MainWindow object;
              object.addition();
              }
              @[/quote]This creates a NEW MainWindow, and then calls addition() on your new MainWindow instance -- it doesn't interact with your old MainWindow.

              Furthermore, object is a local variable -- it gets destroyed when button_clicked() returns.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                Hi,

                [quote author="Qkato" date="1373983270"]Ok I have been doing C++ and Qt for just 2 months.[/quote]

                If after 2 months you don't understand about Signal & Slots you MUST study more.

                As JKSH said you must also pay attention about local defined variables; IMHO you must study more also about C++

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                1 Reply Last reply
                0

                • Login

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