PDA

View Full Version : How to pass a member data from QMainWindow to QDialog



pollausen85
10th October 2013, 00:06
Hi everyone!

I'm a new member of forum and I'm a beginner with Qt. I searched in forum if somebody else had the same problem, but I found the solution only for the reverse problem.

My problem is the following:

I have a QMainWindow (Qsem2D) with some member data and with a button which when is clicked open a QDialog (SettingSolver) window that is a child of QMainWindow.

Qsem2D:

Qsem2D.h


#include <QMainWindow>

class Qsem2d : public QMainWindow
{
.....
private:
bool *m_isForward;

signals:
void signalIsForward(bool i_isForward);

private slots:
void on_pushButton_clicked();

void sendSignalToSettingSolver();

.....
}

Qsem2D.cpp


#include Qsem2D.h
#include SolverSetting.h
....

void Qsem2D::on_pushButton_clicked()
{
sendSignalToSettingSolver();
SolverSetting *newWindow = new SolverSetting;
newWindow->show;
}

void Qsem2D::sendSignalToSettingSolver()
{
emit signalIsForward(*m_isForward);
}

SettingSolver:

SettingSolver.h


#include "Qsem2D.h"
#include <QDialog>

class SettingSolver : public QDialog
{
public:

SettingSolver(Qsem2D *parent = 0);
~SettingSolver();
.....

private:
bool *m_isForward;

private slots:

void receiveSignalFromMainWindow(bool i_isForward);
}

SettingSolver.cpp


#include "SettingSolver.h"
#include <QDialog>

SettingSolver::SettingSolver(Qsem2D *parent):
QDialog(parent),
ui(new Ui::SettingSolver),
m_isForward(new bool)
{
.....
connect(parent,SIGNAL(signalIsForward),this,SLOT(r eceiveSignalFromMainWindow)
if(*m_isForward)
{
....
}
.....
}

void SolverSetting::receiveSignalFromMainWindow(bool i_isForward)
{
*m_isForward = i_isForward;
}


The code could contain some errors because I don't have the original code handy but what I want understand is the concept.

My need is to pass the member data Qsem2D::m_isForward from MainWindow to member data SolverSetting::m_isForward (QDialog). For how the code is made now when I call the constructor for SolverSetting the member data SolverSetting::m_isForward is a pointer that points to memory allocation with no sense information instead I wish that it contains the value of Qsem2D::m_isForward.

What's wrong?

Thanks.

Regards.

toufic.dbouk
10th October 2013, 01:01
Pass it through the constructor when you create your dialog's instance, that's one way. Just rewrite your dialog's constructor to take whatever parameter you want.
The other way could be by making a getter in your dialog and call it after you create your dialog and before you show it for example depending on your need.
Good Luck.

pollausen85
10th October 2013, 10:33
Thanks toufic.dbouk, your solution to pass it through the constructor solved my problem.

Bye!

anda_skoa
11th October 2013, 11:40
The problem in your original code is that you do not pass a parent, thus the connect fails, and that you emit the signal before there is any receiver.

Cheers,
_