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. [RESOLVED] Signal from form to form
Forum Updated to NodeBB v4.3 + New Features

[RESOLVED] Signal from form to form

Scheduled Pinned Locked Moved General and Desktop
18 Posts 5 Posters 5.9k 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.
  • M Offline
    M Offline
    mrcgnerd
    wrote on last edited by
    #9

    Thank you for help ! Your example is realy helpful.

    The program compiles, but not starts. I am geting this line:
    The program has unexpectedly finished.
    But no error in code. What I am doing wrong ?

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QObject::connect(mdialog, SIGNAL(somthingHasHappened(void)), this, SLOT(handleSomthingFromDialog(void)));

    }
    void MainWindow::handleSomthingFromDialog(void)
    {
    ui->Form1Button->setText("YES");
    }@

    I tried to make mdialog public in mainwindow.h:
    @public:
    Dialog *mdialog;@
    And now program starts but when I press radio buton nothing hapens.

    @void MyDialog::on_radioButton_clicked()
    {
    emit somthingHasHappened();
    }@

    1 Reply Last reply
    0
    • IamSumitI Offline
      IamSumitI Offline
      IamSumit
      wrote on last edited by
      #10

      hii
      The program has unexpectedly finished.
      it might be because you did not initialized mdialog (which is pointer) .
      view thread-->
      https://qt-project.org/forums/viewthread/19319

      Be Cute

      1 Reply Last reply
      0
      • C Offline
        C Offline
        code_fodder
        wrote on last edited by
        #11

        yeah, I agree with IamSumit, sounds like a pointer-y like error! (sudden crash - generally means bad memory access).

        You must instantiate (create an instance of) mdiaglog before you connect it, or use it in any way at all :)

        When you do this, check you debug/output if the connection fails (a slot or signal is missing or not correct) then Qt prints a warning.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrcgnerd
          wrote on last edited by
          #12

          I started debug. And it shows :

          "The iferior stopped becouse it recieved a signal from the Operating System.
          Signal name: SIGSEGV
          Signal meaning: Segmentation fault"

          I don't uderstund how corectly initialize mdialog. Is this not corect:

          @public:
          Dialog *mdialog;
          // with this program started but signal not worked
          //But after debuging it's not even starting anymore@

          I tried this:
          @private:
          Dialog *mdialog;
          // Same result@

          And:

          @#include<QDialog>
          Publlic:
          QDialog* mdialog;
          //Same again
          @

          EDIT:

          OK I tried this:
          @mdialog = new Dialog(this);
          QObject::connect(mdialog, SIGNAL(somthingHasHappened(void)), this, SLOT(handleSomthingFromDialog(void)));
          @
          And the program starts again but signal not working, I open dialog pres radio button and nothin is happening.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            code_fodder
            wrote on last edited by
            #13

            What about the connection debug? - do you see some failure with the connection in the debug output? (when you run the program in Qt Creator it should come up in the debug window.

            If you do not, then please print out all code in both files, header and cpp...
            Have you declared the signal somthingHasHappened(void) in mdialog? and implemented handleSomthingFromDialog(void) slot?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrcgnerd
              wrote on last edited by
              #14

              There is no error in debug window.

              My code:
              dialog.h
              @#ifndef DIALOG_H
              #define DIALOG_H

              #include <QDialog>

              namespace Ui {
              class Dialog;
              }

              class Dialog : public QDialog
              {
              Q_OBJECT

              public:
              explicit Dialog(QWidget *parent = 0);
              ~Dialog();
              void processText();

              signals:
              void somthingHasHappened(void);

              private slots:

              void on_radioButton_clicked();
              
              void on_pushButton_clicked();
              

              private:
              Ui::Dialog *ui;
              };

              #endif // DIALOG_H
              @

              dialog.cpp

              @#include "dialog.h"
              #include "ui_dialog.h"

              Dialog::Dialog(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::Dialog)
              {
              ui->setupUi(this);
              }

              Dialog::~Dialog()
              {
              delete ui;
              }

              void Dialog::on_radioButton_clicked()
              {
              emit somthingHasHappened();
              }

              void Dialog::on_pushButton_clicked()
              {
              emit somthingHasHappened();

              }@

              mainwindow.h

              @#ifndef MAINWINDOW_H
              #define MAINWINDOW_H

              #include <QMainWindow>
              #include <dialog.h>
              #include <QDialog>
              #include <QtGui>
              #include <QtCore>

              namespace Ui {
              class MainWindow;
              }

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              explicit MainWindow(QWidget parent = 0);
              ~MainWindow();
              Dialog
              mdialog;

              public slots:
              void handleSomthingFromDialog(void);

              private slots:
              void on_pushButton_clicked();

              private:

              Ui::MainWindow *ui;
              

              };

              #endif // MAINWINDOW_H
              @

              mainwindow.cpp

              @#include "mainwindow.h"
              #include "ui_mainwindow.h"

              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
              {
              ui->setupUi(this);
              mdialog = new Dialog(this);
              QObject::connect(mdialog, SIGNAL(somthingHasHappened(void)), this, SLOT(handleSomthingFromDialog(void)));

              }
              void MainWindow::handleSomthingFromDialog(void)
              {
              ui->textEdit->setText("YES");
              }

              MainWindow::~MainWindow()
              {
              delete ui;
              }

              void MainWindow::on_pushButton_clicked()
              {
              Dialog mydialog;
              mydialog.exec();

              //mdialog = new Dialog(this);
              //mdialog->show();

              }@

              main.cpp

              @#include "mainwindow.h"
              #include <QApplication>

              int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();

              return a.exec&#40;&#41;;
              

              }
              @

              1 Reply Last reply
              0
              • C Offline
                C Offline
                code_fodder
                wrote on last edited by
                #15

                Can you put some debug in
                void Dialog::on_radioButton_clicked()
                void MainWindow::handleSomthingFromDialog(void)

                to see if one or both functions are getting called?

                the signal / slots look good to me.

                However:
                What is this:
                @
                void MainWindow::on_pushButton_clicked()
                {
                Dialog mydialog;
                mydialog.exec();

                //mdialog = new Dialog(this);
                //mdialog->show();

                }
                @

                A second dialog? - if this is the dialog that is running, then it is a different instance of the dialog that you have connected your signal/slot to.

                mydialog != mdialog

                You probably want to do
                @
                mdialog->exec();
                @

                this is a guess because I don't know the order of how you do things.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  code_fodder
                  wrote on last edited by
                  #16

                  or do:

                  @
                  void MainWindow::on_pushButton_clicked()
                  {
                  Dialog mydialog;

                  QObject::connect(&mydialog, SIGNAL(somthingHasHappened(void)), this, SLOT(handleSomthingFromDialog(void)));
                  

                  mydialog.exec();
                  }
                  @

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrcgnerd
                    wrote on last edited by
                    #17

                    [quote author="code_fodder" date="1393943815"]or do:

                    @
                    void MainWindow::on_pushButton_clicked()
                    {
                    Dialog mydialog;

                    QObject::connect(&mydialog, SIGNAL(somthingHasHappened(void)), this, SLOT(handleSomthingFromDialog(void)));
                    

                    mydialog.exec();
                    }
                    @[/quote]

                    YES ! :DDDD
                    It's working !

                    THANK YOU !
                    You helped alot. I think now I will use signals and slots more often in my projects, they are realy useful. I will read more about it.

                    One last thing, this can sound stupid, becouse I think this is not even Qt but C++ related, but what does & sign before mydialog, in conection line mean ? (connect(&mydialog) ?
                    Sory I just learning stuf and not facetd with this before.

                    By the way how can I mark subject as SOLVED ? (God it's even more stupid question :D)

                    Thanks again for everyone, and sory for my english.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      code_fodder
                      wrote on last edited by
                      #18

                      Ah, that is because you have to pass in a pointer in the connect() function.

                      Dialog mydialog; - is a variable of type Dialog.
                      Dialog *mdialog; - is a pointer (address) to a Dialog variable.

                      So to make pass the address of a variable you have to use to the "&" operator, therefore &mydialog becomes (Dialog *) type (address of).

                      To mark this thread as resolved, just change the title of your post to:
                      "[RESOLVED] Signal from form to form"

                      This is the convention

                      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