PDA

View Full Version : To display output of a function from mainform to any other new form



nawaz
29th January 2013, 18:04
Hello all

i want to display ouput of a function defined in mainform(binary) to next form (Analyze) . means whn i press push button 2 output displayed on line edit of next form(Analyze)?? how m able to do that plz Help???

binary.cpp




#include "binary.h"
#include "ui_binary.h"
#include "ui_analyze.h"

binary::binary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::binary)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openAnalyze()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));
}







void binary::openAnalyze()
{
myAnalzye= new Analyze();

myAnalyze->show();

}
void binary::text()
{
QString set= "hello";
ui->lineEdit->setText(set);

}



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

d_stranz
29th January 2013, 22:32
Implement a signal in "binary" that is emitted when the button is pushed. The signal would look something like this:



signals:
void button2Pushed( const QString & text );


In your "Analyze" dialog, implement a slot that is connected to that signal:



public slots:
void onButton2Push( const QString & text );


In your "openAnalyze" method, connect the signal to the slot:



connect( this, SIGNAL( onButton2Push( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );


In the binary class text() method, emit the signal:



emit button2Pushed( set );


By the way, in your code line 22 is a memory leak.

nawaz
30th January 2013, 06:41
i got following error
binary.h


#ifndef BINARY_H
#define BINARY_H

#include <QMainWindow>
#include"analyze.h"

namespace Ui {
class binary;
}

class binary : public QMainWindow
{
Q_OBJECT

public:
explicit binary(QWidget *parent = 0);
~binary();
public slots:
void openAnalyze();

public slots:
void text();
signals:
void button2Pushed( const QString & text );



private:
Analyze *myAnalyze;



private:
Ui::binary *ui;
};

#endif // BINARY_H





binary.cpp


#include "binary.h"
#include "ui_binary.h"
#include "ui_analyze.h"

binary::binary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::binary)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openAnalyze()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));

}







void binary::openAnalyze()
{
myAnalyze= new Analyze();

myAnalyze->show();
connect( this, SIGNAL( onButton2Push( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );

}
void binary::text()
{
QString set= "hello";
ui->lineEdit->setText(set);
emit button2Pushed( set );
}



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





analyze.h


#ifndef ANALYZE_H
#define ANALYZE_H

#include <QDialog>

namespace Ui {
class Analyze;
}

class Analyze : public QDialog
{
Q_OBJECT

public:
explicit Analyze(QWidget *parent = 0);
~Analyze();

public slots:
void onButton2Push( const QString & text );


private:
Ui::Analyze *ui;
};

#endif // ANALYZE_H



analyze.cpp


#include "analyze.h"
#include "ui_analyze.h"

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

}
void Analyze::onButton2Push( const QString & text )
{
}



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




i gott following error plz help
starting /home/nawazbaig/BinaryAnalyzer-build-desktop-Qt_4_8_1_in_PATH__System__Release/BinaryAnalyzer...
Object::connect: No such signal binary::onButton2Push( const QString & )
Object::connect: (sender name: 'binary')
Object::connect: (receiver name: 'Analyze')
/home/nawazbaig/BinaryAnalyzer-build-desktop-Qt_4_8_1_in_PATH__System__Release/BinaryAnalyzer exited with code 0

plz help i displayed code with changes but no result

d_stranz
30th January 2013, 16:14
Object::connect: No such signal binary:: onButton2Push( const QString & )


So? Qt is telling you the truth. There is no binary:: onButton2Push() signal.
What does line 24 in your binary.h code say?


void button2Pushed( const QString & text );

And line 23 in binary.cpp is still a memory leak. If you don't see why, think about what happens the second time that openAnalyze() is called.

nawaz
30th January 2013, 17:21
HI
I am pretty new plz tell me how i overcome this memory leak plz ?????

i correct button2Pushed
plz help

No error but no ouput on analyze dialog when i press push button 2 frombinary

Binary.h



#ifndef BINARY_H
#define BINARY_H

#include <QMainWindow>
#include"analyze.h"

namespace Ui {
class binary;
}

class binary : public QMainWindow
{
Q_OBJECT

public:
explicit binary(QWidget *parent = 0);
~binary();
public slots:
void openAnalyze();
public slots:
void text();
signals:
void button2Pushed( const QString & text );



private:
Analyze *myAnalyze;



private:
Ui::binary *ui;
};

#endif // BINARY_H



Binary.cpp


#include "binary.h"
#include "ui_binary.h"
#include "ui_analyze.h"

binary::binary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::binary)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openAnalyze()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));

}







void binary::openAnalyze()
{
myAnalyze= new Analyze();

myAnalyze->show();
connect( this, SIGNAL( button2Pushed( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );

}
void binary::text()
{
QString set= "hello";

emit button2Pushed( set );
}



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




Analyze.h



#ifndef ANALYZE_H
#define ANALYZE_H

#include <QDialog>

namespace Ui {
class Analyze;
}

class Analyze : public QDialog
{
Q_OBJECT

public:
explicit Analyze(QWidget *parent = 0);
~Analyze();

public slots:
void onButton2Push( const QString & text );


private:
Ui::Analyze *ui;
};

#endif // ANALYZE_H




Analyze.cpp


#include "analyze.h"
#include "ui_analyze.h"

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

}
void Analyze::onButton2Push( const QString & text )
{

}




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

d_stranz
30th January 2013, 17:35
void Analyze::onButton2Push( const QString & text )
{

}

Of course there's no output. You aren't doing anything in this slot. Do you think that by some magic, Qt is supposed to know that "text" should appear in some line edit on the Analyze dialog? You have to tell it where to put the text if you want it to appear. (Don't ask how to do that. If you don't understand something so simple, go back and study some Qt examples until you understand it. We aren't here to write your programs for you).

Fix the memory leak by moving the "myAnalyze = new Analyze();" line into the constructor for the binary class (and change it to "myAnalyze = new Analyze( this )" so the instance gets destroyed when binary is destroyed), and move the "connect" there also, after the Analyze instance is created. That way, you create only one instance of Analyze, and you use the same one each time openAnalyze() is called.

nawaz
30th January 2013, 18:08
Thanks...a lot
for ur help

d_stranz
1st February 2013, 01:36
So if you rewrite your constructor this way, you do not need the "openAnalyze()" slot at all:


binary::binary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::binary)
{
ui->setupUi(this);

myAnalyze = new Analyze( this );

connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));
connect(ui->pushButton, SIGNAL(clicked()), myAnalyze, SLOT( show() ) );
connect( this, SIGNAL( button2Pushed( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );
}


That is, you can connect the pushbutton's clicked signal() directly to the myAnalyze show() slot, because after you move the code around, there isn't anything except a call to show() in the openAnalyze() slot. You can get rid of it.