[RESOLVED] Signal from form to form
-
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();
}@ -
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 -
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.
-
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. -
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? -
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_OBJECTpublic:
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_OBJECTpublic:
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();
}
@ -
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.
-
or do:
@
void MainWindow::on_pushButton_clicked()
{
Dialog mydialog;QObject::connect(&mydialog, SIGNAL(somthingHasHappened(void)), this, SLOT(handleSomthingFromDialog(void)));
mydialog.exec();
}
@ -
[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.
-
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