Results 1 to 8 of 8

Thread: To display output of a function from mainform to any other new form

  1. #1
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default To display output of a function from mainform to any other new form

    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
    Qt Code:
    1. #include "binary.h"
    2. #include "ui_binary.h"
    3. #include "ui_analyze.h"
    4.  
    5. binary::binary(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::binary)
    8. {
    9. ui->setupUi(this);
    10. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openAnalyze()));
    11. connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));
    12. }
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20. void binary::openAnalyze()
    21. {
    22. myAnalzye= new Analyze();
    23.  
    24. myAnalyze->show();
    25.  
    26. }
    27. void binary::text()
    28. {
    29. QString set= "hello";
    30. ui->lineEdit->setText(set);
    31.  
    32. }
    33.  
    34.  
    35.  
    36. binary::~binary()
    37. {
    38. delete ui;
    39. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,794
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: To display output of a function from mainform to any other new form

    Implement a signal in "binary" that is emitted when the button is pushed. The signal would look something like this:

    Qt Code:
    1. signals:
    2. void button2Pushed( const QString & text );
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. public slots:
    2. void onButton2Push( const QString & text );
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. connect( this, SIGNAL( onButton2Push( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. emit button2Pushed( set );
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: To display output of a function from mainform to any other new form

    i got following error
    binary.h
    Qt Code:
    1. #ifndef BINARY_H
    2. #define BINARY_H
    3.  
    4. #include <QMainWindow>
    5. #include"analyze.h"
    6.  
    7. namespace Ui {
    8. class binary;
    9. }
    10.  
    11. class binary : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit binary(QWidget *parent = 0);
    17. ~binary();
    18. public slots:
    19. void openAnalyze();
    20.  
    21. public slots:
    22. void text();
    23. signals:
    24. void button2Pushed( const QString & text );
    25.  
    26.  
    27.  
    28. private:
    29. Analyze *myAnalyze;
    30.  
    31.  
    32.  
    33. private:
    34. Ui::binary *ui;
    35. };
    36.  
    37. #endif // BINARY_H
    To copy to clipboard, switch view to plain text mode 



    binary.cpp
    Qt Code:
    1. #include "binary.h"
    2. #include "ui_binary.h"
    3. #include "ui_analyze.h"
    4.  
    5. binary::binary(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::binary)
    8. {
    9. ui->setupUi(this);
    10. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openAnalyze()));
    11. connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));
    12.  
    13. }
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21. void binary::openAnalyze()
    22. {
    23. myAnalyze= new Analyze();
    24.  
    25. myAnalyze->show();
    26. connect( this, SIGNAL( onButton2Push( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );
    27.  
    28. }
    29. void binary::text()
    30. {
    31. QString set= "hello";
    32. ui->lineEdit->setText(set);
    33. emit button2Pushed( set );
    34. }
    35.  
    36.  
    37.  
    38. binary::~binary()
    39. {
    40. delete ui;
    41. }
    To copy to clipboard, switch view to plain text mode 

    analyze.h
    Qt Code:
    1. #ifndef ANALYZE_H
    2. #define ANALYZE_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class Analyze;
    8. }
    9.  
    10. class Analyze : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Analyze(QWidget *parent = 0);
    16. ~Analyze();
    17.  
    18. public slots:
    19. void onButton2Push( const QString & text );
    20.  
    21.  
    22. private:
    23. Ui::Analyze *ui;
    24. };
    25.  
    26. #endif // ANALYZE_H
    To copy to clipboard, switch view to plain text mode 

    analyze.cpp
    Qt Code:
    1. #include "analyze.h"
    2. #include "ui_analyze.h"
    3.  
    4. Analyze::Analyze(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Analyze)
    7. {
    8. ui->setupUi(this);
    9.  
    10. }
    11. void Analyze::onButton2Push( const QString & text )
    12. {
    13. }
    14.  
    15.  
    16.  
    17. Analyze::~Analyze()
    18. {
    19. delete ui;
    20. }
    To copy to clipboard, switch view to plain text mode 


    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:nButton2Push( 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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,794
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: To display output of a function from mainform to any other new form

    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?

    Qt Code:
    1. void button2Pushed( const QString & text );
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #5
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: To display output of a function from mainform to any other new form

    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

    Qt Code:
    1. #ifndef BINARY_H
    2. #define BINARY_H
    3.  
    4. #include <QMainWindow>
    5. #include"analyze.h"
    6.  
    7. namespace Ui {
    8. class binary;
    9. }
    10.  
    11. class binary : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit binary(QWidget *parent = 0);
    17. ~binary();
    18. public slots:
    19. void openAnalyze();
    20. public slots:
    21. void text();
    22. signals:
    23. void button2Pushed( const QString & text );
    24.  
    25.  
    26.  
    27. private:
    28. Analyze *myAnalyze;
    29.  
    30.  
    31.  
    32. private:
    33. Ui::binary *ui;
    34. };
    35.  
    36. #endif // BINARY_H
    To copy to clipboard, switch view to plain text mode 

    Binary.cpp
    Qt Code:
    1. #include "binary.h"
    2. #include "ui_binary.h"
    3. #include "ui_analyze.h"
    4.  
    5. binary::binary(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::binary)
    8. {
    9. ui->setupUi(this);
    10. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openAnalyze()));
    11. connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));
    12.  
    13. }
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21. void binary::openAnalyze()
    22. {
    23. myAnalyze= new Analyze();
    24.  
    25. myAnalyze->show();
    26. connect( this, SIGNAL( button2Pushed( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );
    27.  
    28. }
    29. void binary::text()
    30. {
    31. QString set= "hello";
    32.  
    33. emit button2Pushed( set );
    34. }
    35.  
    36.  
    37.  
    38. binary::~binary()
    39. {
    40. delete ui;
    41. }
    To copy to clipboard, switch view to plain text mode 


    Analyze.h

    Qt Code:
    1. #ifndef ANALYZE_H
    2. #define ANALYZE_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class Analyze;
    8. }
    9.  
    10. class Analyze : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Analyze(QWidget *parent = 0);
    16. ~Analyze();
    17.  
    18. public slots:
    19. void onButton2Push( const QString & text );
    20.  
    21.  
    22. private:
    23. Ui::Analyze *ui;
    24. };
    25.  
    26. #endif // ANALYZE_H
    To copy to clipboard, switch view to plain text mode 


    Analyze.cpp
    Qt Code:
    1. #include "analyze.h"
    2. #include "ui_analyze.h"
    3.  
    4. Analyze::Analyze(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Analyze)
    7. {
    8. ui->setupUi(this);
    9.  
    10. }
    11. void Analyze::onButton2Push( const QString & text )
    12. {
    13.  
    14. }
    15.  
    16.  
    17.  
    18.  
    19. Analyze::~Analyze()
    20. {
    21. delete ui;
    22. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,794
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: To display output of a function from mainform to any other new form

    Qt Code:
    1. void Analyze::onButton2Push( const QString & text )
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  7. The following user says thank you to d_stranz for this useful post:

    nawaz (30th January 2013)

  8. #7
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: To display output of a function from mainform to any other new form

    Thanks...a lot
    for ur help

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,794
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: To display output of a function from mainform to any other new form

    So if you rewrite your constructor this way, you do not need the "openAnalyze()" slot at all:
    Qt Code:
    1. binary::binary(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::binary)
    4. {
    5. ui->setupUi(this);
    6.  
    7. myAnalyze = new Analyze( this );
    8.  
    9. connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(text()));
    10. connect(ui->pushButton, SIGNAL(clicked()), myAnalyze, SLOT( show() ) );
    11. connect( this, SIGNAL( button2Pushed( const QString & ) ), myAnalyze, SLOT( onButton2Push( const QString & ) ) );
    12. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. UNABLE TO DISPLAY OUTPUT USING QTextStream
    By ayanda83 in forum Newbie
    Replies: 3
    Last Post: 11th July 2012, 09:15
  2. Urgent : Form updates not displayed on the output
    By seanasl in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2012, 15:51
  3. Display new form
    By obad in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2010, 13:57
  4. Replies: 3
    Last Post: 18th October 2007, 19:07
  5. how to write a template function in a form..
    By nass in forum General Programming
    Replies: 7
    Last Post: 18th December 2006, 20:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.