Results 1 to 5 of 5

Thread: Problem with signals slots and emit

  1. #1
    Join Date
    Sep 2011
    Location
    Buenos Aires
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Problem with signals slots and emit

    Hello.
    I'm currently learning how to program in c++ with qt and I'm having a problem when trying to emit a signal to a second mainwindow.
    The whole idea is to emit a signal inside a function in the first form and catch it in the second form to add a line to a textEdit.
    The idea is to use the second form as some type of debug console, there i want to show debuggin messages so to avoid having a system console open to show the qDebug messages and to provide a save and report mechanism to the messages.

    I wrote this simple example to demostrate my problem.

    main.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "first.h"
    3. #include "second.h"
    4.  
    5. int main(int argc, char** argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. first f;
    10. f.show();
    11.  
    12. second s;
    13. s.show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    first.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "first.h"
    4. #include "second.h"
    5.  
    6. first::first()
    7. {
    8.  
    9. QPushButton *button = new QPushButton("First", this);
    10. setCentralWidget( button );
    11.  
    12.  
    13. QString text = "test";
    14. connect( button, SIGNAL ( clicked() ) ,this, SLOT( doemit() ));
    15.  
    16. //if i uncomment the next two lines the program stales and never shows the forms
    17. //second s;
    18. //connect( this, SIGNAL(MySignal(QString)), &s, SLOT( add() ));
    19.  
    20. }
    21.  
    22. first::~first()
    23. {}
    24.  
    25. void first::doemit() {
    26.  
    27. qDebug() << "doemit called";
    28.  
    29. QString test = "text";
    30. emit MySignal(test);
    31.  
    32. }
    33.  
    34. #include "first.moc"
    To copy to clipboard, switch view to plain text mode 

    first.h
    Qt Code:
    1. #ifndef first_H
    2. #define first_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6. class first : public QMainWindow
    7. {
    8. Q_OBJECT
    9. public:
    10. first();
    11. virtual ~first();
    12.  
    13. public slots:
    14. void doemit();
    15.  
    16. signals:
    17. void MySignal( QString );
    18. };
    19.  
    20. #endif // first_H
    To copy to clipboard, switch view to plain text mode 

    second.cpp
    Qt Code:
    1. #include "second.h"
    2. #include "first.h"
    3.  
    4. #include <QtGui>
    5. #include <QTextEdit>
    6.  
    7. second::second() {
    8.  
    9. textEdit = new QTextEdit;
    10. setCentralWidget(textEdit);
    11.  
    12. first f;
    13. connect(&f, SIGNAL(MySignal(QString)), this, SLOT(add()));
    14. }
    15.  
    16. second::~second(){}
    17.  
    18. void second::add(){
    19. qDebug() << "Slot Called";
    20. textEdit->append ( "Blah" );
    21. }
    22.  
    23. #include "second.moc"
    To copy to clipboard, switch view to plain text mode 

    second.h
    Qt Code:
    1. #ifndef second_H
    2. #define second_H
    3.  
    4. #include <QtGui>
    5. #include <QTextEdit>
    6.  
    7. class second : public QMainWindow
    8. {
    9. Q_OBJECT
    10. public:
    11. second();
    12. virtual ~second();
    13. QTextEdit *textEdit;
    14.  
    15. public slots:
    16. void add();
    17. };
    18.  
    19. #endif // second_H
    To copy to clipboard, switch view to plain text mode 

    What happens with the program is that doemit() is called in first.cpp but never reaches the slot in second.cpp.
    if i try to reverse the connection, as in commenting the connect line in second.cpp and uncommenting the connect in first the same happens
    but if i leave both connects the program starts but nothing is show in screen.

    Maybe i can't have to mainwindows as one blocks the other and i need to dig into threading, but i would like to avoid that.

    I hope that my problem is clear, as I'm not a native english speaker maybe my writing is a little cryptic at best.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with signals slots and emit

    The connection has to be established in main where the actual second instance is! It couldn't work any other way...


    ...and
    Qt Code:
    1. first f;
    2. connect(&f, SIGNAL(MySignal(QString)), this, SLOT(add()));
    To copy to clipboard, switch view to plain text mode 
    is nonsense since f gets destroyed at the scope end. (Basic C++)

  3. The following user says thank you to Lykurg for this useful post:

    slqh (19th September 2011)

  4. #3
    Join Date
    Sep 2011
    Location
    Buenos Aires
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SOLVED] Re: Problem with signals slots and emit

    Quote Originally Posted by Lykurg View Post
    The connection has to be established in main where the actual second instance is! It couldn't work any other way...
    Thank you.
    That was a simple solution, I thought that connect() could only be called from inside the from constructor.
    As the examples I read were consistently doing the connects there.


    As for the Basic c++ error.
    C++ has proved to be a really complex language to learn, at least for me.
    As i get better I hope to have less stuff like that or at least leave them out of the forum questions!

    Thank you for your time.

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [SOLVED] Re: Problem with signals slots and emit

    Quote Originally Posted by slqh View Post
    Thank you.
    That was a simple solution, I thought that connect() could only be called from inside the from constructor.
    As the examples I read were consistently doing the connects there.
    They are doing it there because they are defining the objects there. You can call connect everywhere you like, but you must have valid pointers to the objects you like to connect. In your case you created a new instance of first/second and used that. But this instances aren't identical with that ones you have created in main function and which you actually use.

  6. The following user says thank you to Lykurg for this useful post:

    slqh (19th September 2011)

  7. #5
    Join Date
    Sep 2011
    Location
    Buenos Aires
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problem with signals slots and emit

    They are doing it there because they are defining the objects there. You can call connect everywhere you like, but you must have valid pointers to the objects you like to connect. In your case you created a new instance of first/second and used that. But this instances aren't identical with that ones you have created in main function and which you actually use.
    Man not only did you help me with my problem it looks like you can read minds.
    i was looking againt at the working code to see exactly what was the root of my problem.
    And an after a refresh y see this.

    I understand now that i need to pass the same object to the connect than i used to show() the form.
    creating a new instance does not work because well is a new instance and not the one i was seeing.

    Again. Thank you.

    Edit. Also for future reference this is the working way.

    Qt Code:
    1. MainWindow win;
    2. win.resize(1024, 768);
    3. win.show();
    4.  
    5. debug deb;
    6. deb.show();
    7. QObject::connect(&win, SIGNAL( MySignal() ),&deb, SLOT( MySlot() ));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. problem with signals/slots
    By vaas in forum Newbie
    Replies: 3
    Last Post: 2nd April 2010, 15:24
  2. Signals and Slots Problem
    By GenericProdigy in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2009, 10:06
  3. Problem using SIGNALS/SLOTS
    By JimDaniel in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2007, 05:59
  4. Problem with signals and slots
    By conexion2000 in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2006, 11:20
  5. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 12:35

Tags for this Thread

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.