Page 1 of 2 12 LastLast
Results 1 to 20 of 33

Thread: Signal can't be emitted !

  1. #1
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Signal can't be emitted !

    Hello,

    file1.h
    Qt Code:
    1. class MyServer : public QTcpServer
    2. {
    3. Q_OBJECT
    4.  
    5. .... // some code
    6.  
    7. signals:
    8. void _connection(int);
    9.  
    10. .... // rest of the code
    To copy to clipboard, switch view to plain text mode 


    file1.cpp
    Qt Code:
    1. void MyServer::incomingConnection(int handle)
    2. {
    3. MyClient *client = new MyClient(this);
    4. client->setsocket(handle);
    5. emit _connection(handle); // i emit it here
    6. }
    To copy to clipboard, switch view to plain text mode 

    file2.h
    Qt Code:
    1. #include "file1.h"
    2. class Blabla: public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. ... // code here
    7.  
    8. private:
    9. MyServer *server;
    10.  
    11. ... // rest of code
    To copy to clipboard, switch view to plain text mode 

    file2.cpp
    Qt Code:
    1. server = new MyServer;
    2.  
    3. connect(server,SIGNAL(_connection()), this, SLOT(connected())); // the slot is declared in the code
    To copy to clipboard, switch view to plain text mode 

    The issue:
    ----------
    The signal is not emitted within the incoming connction.

    The question:
    -------------
    Why ?


    Thanks,
    Vladimir.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Signal can't be emitted !

    The code is never called. You are not running the code you think you are. You have not connected to the signal or object you think you have. There is no incoming connection, perhaps blocked by a firewall. Etc.

    What have you done to debug this? Put a breakpoint in the routine? Single stepped?

  3. #3
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    there's an incoming connection,

    Qt Code:
    1. void MyServer::incomingConnection(int handle)
    2. {
    3. MyClient *client = new MyClient(this);
    4. client->setsocket(handle);
    5. QMessageBox::information(0, "connected", "new client"); // this runs, so the function worked.
    6. emit _connection(handle); // i emit it here
    7. }
    To copy to clipboard, switch view to plain text mode 

    the incomingConnection() is written in italic in the QtCreator. it's already coded.

    here's a proof that the code worked:
    000000000000000000000000000000000000000000000000000000000.png

    but the signal is never emitted.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal can't be emitted !

    How do you know the signal is not emitted? Maybe a more appropriate topic to your problem would be "slot is not called" rather than "signal is not emitted"? Can you call connected() using e.g. invokeMethod()? Do the sender and receiver belong to the same thread?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    I used another signal, and the slot is called.

    So i made sure that the slot has no issues .

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal can't be emitted !

    Is the connection established properly? Does connect() return true? Do you get any messages, warnings on the console when your program is running?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    no messages or warnings at all !

    the connection istablished well, and i even get the sent data via QByteArray and display it in QMessageBox.

    only this can't be emitted !!

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal can't be emitted !

    Quote Originally Posted by Vladimir_ View Post
    file2.cpp
    Qt Code:
    1. server = new MyServer;
    2.  
    3. connect(server,SIGNAL(_connection()), this, SLOT(connected())); // the slot is declared in the code
    To copy to clipboard, switch view to plain text mode 

    The issue:
    ----------
    The signal is not emitted within the incoming connction.
    The signal is not emitted because it does not exists.

    The application output would have told you that (Qt prints a warning) and the return value of the connect would have been "false".

    Look at the content of the SIGNAL macro and the declaration of the signal and find the difference.

    Cheers,
    _


    P.S.: unless you need SSL sockets, there is no need to derive from QTcpServer. I has a signal that is emitted for each incoming connection.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal can't be emitted !

    Quote Originally Posted by Vladimir_ View Post
    no messages or warnings at all !
    I'm sure you are wrong about this. Maybe you are looking in the wrong place. We are talking about a runtime message to the console, not a warning during compilation.

    the connection istablished well
    Does connect() return true? I don't think so.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    connect() returns 0 , means "false" ....

    What an I do now ?!

    i posted all the codes I did, i need it working

    Quote Originally Posted by anda_skoa View Post
    The signal is not emitted because it does not exists.
    see file1.h and file1.cpp .

    what do you mean it does not exist ?

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal can't be emitted !

    1. Is the class with method connected() derived from QObject ?
    2. Is the method connected declared as slot ?

  12. #12
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    myclient.h
    Qt Code:
    1. class MyClient : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MyClient(QObject *parent = 0);
    6. void setsocket(int Descriptor);
    7.  
    8.  
    9. QTcpSocket *socket;
    10.  
    11. signals:
    12. // can't put signal here, coz [I]incomingConnection()[/I] isn't here.
    13. public slots:
    14. void connected(); // here it is
    15. void disconncted();
    16. void readyRead();
    17. void TaskResult(int number);
    18. void setNum();
    19. //.... rest of the code
    To copy to clipboard, switch view to plain text mode 

    myclient.cpp
    Qt Code:
    1. void MyClient::connected()
    2. {
    3. QMessageBox::information(0, "connected", "bingo!");
    4. }
    To copy to clipboard, switch view to plain text mode 


    Added after 8 minutes:


    Okay guys .. I came up with the solution.

    SIGNALS can't be emitted in the incomingConnection() function.

    Maybe because it's not called manually .

    I tried to create a method and emitted the signal in it.

    I called the method manually then put the connect() statement .. IT WORKED!!

    summary:
    ---------
    Don't emit signals in methods which is not called by you manually in the code.

    Thanks,
    Vladimir.
    Last edited by Vladimir_; 26th September 2014 at 16:44.

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal can't be emitted !

    Quote Originally Posted by Vladimir_ View Post
    Okay guys .. I came up with the solution.
    No, you just avoided the mistake you made.

    Quote Originally Posted by Vladimir_ View Post
    SIGNALS can't be emitted in the incomingConnection() function.
    Of course that is possible.
    That was not the problem.

    You problem was, as I already wrote earlier, that you tried to connect to a signal that did not exists.

    If you had, as I suggested, checked the difference between the signal declaration and the non existing signal used in the SIGNAL macro, you would have seen that your signal had the signature
    Qt Code:
    1. _connection(int)
    To copy to clipboard, switch view to plain text mode 
    but you were trying to connect to
    Qt Code:
    1. _connection()
    To copy to clipboard, switch view to plain text mode 

    You can of course decide to remain ignorant to how Qt's signal/slot feature works and make up some silly rules instead.

    Cheers,
    _

  14. #14
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    anda_soka, now I called :

    Qt Code:
    1. int cn = connect(server,SIGNAL(_connection(int)), this, SLOT(connected()));
    2. QMessageBox::information(0, "connect()", QString::number(cn));
    To copy to clipboard, switch view to plain text mode 

    the messagebox shows e that cn = 1, means connect() returns "true" after it was returning 0 "false" .

    But .. the SLOT didn't work ..

    EDIT: I changed to another slut , but it still won't work
    Last edited by Vladimir_; 26th September 2014 at 17:16.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal can't be emitted !

    "Doesn't work" is really not helpful description of a problem.

    If you want us to test something, I suggest you provide a minimal compilable example reproducing the problem (10-20 lines of code).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    wysota, i gave all the codes in the #1 post of this thread.

    But it's ok. i'll do it again with the WHOLE files "not just a snippets" :

    myserver.h
    Qt Code:
    1. #ifndef MYSERVER_H
    2. #define MYSERVER_H
    3.  
    4. #include <QTcpServer>
    5. #include <QTcpSocket>
    6. #include <QAbstractSocket>
    7. #include "mainwindow.h"
    8.  
    9. class MyServer : public QTcpServer
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit MyServer(QObject *parent = 0);
    14. void startServer(int);
    15.  
    16. protected:
    17. void incomingConnection(int handle);
    18.  
    19. signals:
    20. void _connection(int handle);
    21.  
    22. public slots:
    23.  
    24. private:
    25.  
    26. MainWindow *main;
    27. QTcpSocket *socket;
    28.  
    29.  
    30. };
    31.  
    32. #endif // MYSERVER_H
    To copy to clipboard, switch view to plain text mode 

    myserver.cpp
    Qt Code:
    1. #include "myserver.h"
    2. #include "myclient.h"
    3. #include <QtGui>
    4.  
    5. MyServer::MyServer(QObject *parent) :
    6. QTcpServer(parent)
    7. {
    8. main = new MainWindow;
    9. socket = new QTcpSocket;
    10. }
    11.  
    12. void MyServer::startServer(int port)
    13. {
    14. if(listen(QHostAddress::Any,port))
    15. {
    16.  
    17. QMessageBox::information(0,"started", "Server Started!!");
    18. }
    19. else
    20. {
    21. QMessageBox::warning(0,"Server not started", "Can't listen on the port "+QString::number(port))+".<br>"
    22. "please change the port.<br>This happened because the port is busy.";
    23. }
    24. }
    25.  
    26. void MyServer::incomingConnection(int handle)
    27. {
    28. MyClient *client = new MyClient(this);
    29. client->setsocket(handle);
    30. emit _connection(handle);
    31. }
    To copy to clipboard, switch view to plain text mode 

    myclient.h
    Qt Code:
    1. #ifndef MYCLIENT_H
    2. #define MYCLIENT_H
    3.  
    4. #include <QObject>
    5. #include <QTcpSocket>
    6. #include <QDebug>
    7. #include <QThreadPool>
    8. #include <QtGui>
    9. #include "mytask.h"
    10. #include "myserver.h"
    11. #include "mainwindow.h"
    12.  
    13.  
    14. class MyClient : public QObject
    15. {
    16. Q_OBJECT
    17. public:
    18. explicit MyClient(QObject *parent = 0);
    19. void setsocket(int Descriptor);
    20. void clientConnected();
    21.  
    22.  
    23. QTcpSocket *socket;
    24.  
    25. signals:
    26. void isConnected();
    27.  
    28. public slots:
    29. void connected();
    30. void disconncted();
    31. void readyRead();
    32. void TaskResult(int number);
    33. void setNum();
    34.  
    35. private:
    36. MyClient *client;
    37. MainWindow *main;
    38.  
    39. MyServer *server;
    40.  
    41. };
    42.  
    43. #endif // MYCLIENT_H
    To copy to clipboard, switch view to plain text mode 


    myclient.cpp
    Qt Code:
    1. #include "myclient.h"
    2.  
    3. MyClient::MyClient(QObject *parent) :
    4. QObject(parent)
    5. {
    6. QThreadPool::globalInstance()->setMaxThreadCount(5);
    7.  
    8.  
    9. }
    10.  
    11.  
    12. void MyClient::setsocket(int Descriptor)
    13. {
    14.  
    15. socket = new QTcpSocket;
    16.  
    17. main = new MainWindow;
    18.  
    19. server = new MyServer;
    20.  
    21. clientConnected();
    22.  
    23. int cn = connect(server,SIGNAL(_connection(int)), this, SLOT(connected()));
    24. QMessageBox::information(0, "cinnect()", QString::number(cn));
    25. connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
    26. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    27.  
    28.  
    29.  
    30. socket->setSocketDescriptor(Descriptor);
    31.  
    32.  
    33. }
    34.  
    35.  
    36. void MyClient::connected()
    37. {
    38.  
    39. QMessageBox::information(0, "connected", "bingo!");
    40. }
    41.  
    42. void MyClient::disconncted()
    43. {
    44. qDebug() << "client disconnected!";
    45. // test -= 1;
    46. }
    47.  
    48. void MyClient::clientConnected()
    49. {
    50. QByteArray handshake = socket->readAll();
    51.  
    52. if(handshake == "newClient")
    53. {
    54.  
    55. emit isConnected();
    56.  
    57. }
    58. }
    59.  
    60. void MyClient::readyRead()
    61. {
    62.  
    63.  
    64. // time consumer
    65. MyTask *mytask = new MyTask();
    66. mytask->setAutoDelete(true);
    67. connect(mytask, SIGNAL(Result(int)), this, SLOT(TaskResult(int)), Qt::QueuedConnection);
    68. QThreadPool::globalInstance()->start(mytask);
    69. }
    70.  
    71. void MyClient::TaskResult(int number)
    72. {
    73. QByteArray Buffer;
    74. Buffer.append("Task Result = ");
    75. Buffer.append(QString::number(number));
    76.  
    77. socket->write(Buffer);
    78.  
    79. }
    80.  
    81. void MyClient::setNum()
    82. {
    83. emit isConnected();
    84. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal can't be emitted !

    Quote Originally Posted by Vladimir_ View Post
    wysota, i gave all the codes in the #1 post of this thread.
    Something that doesn't have a main function is certainly not compilable.

    But it's ok. i'll do it again with the WHOLE files "not just a snippets" :
    Still no main() and still not minimal. You are missing the point of this exercise. If you "test" 200 lines of code containing many classes not at all related to the problem you are dealing with then there is too much external influence to focus on the real problem. If you have problems with establishing a signal-slot connection then strip out all code but the one related to that connection. I doubt TaskResult class or a thread pool are going to help you isolate the problem. So either remove code from what you have now or build an example starting with an empty project and filling it with your code. There is a good chance the problem will smack you in your face without us helping.

    By the way I don't think code for setting a maximum number of threads on the default thread pool belongs to a constructor of a TCP client class. Probably also creating a main window in a function for setting a socket descriptor is not the best approach too.

    And also start using qDebug() instead of QMessageBox for getting debug output from your programs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    Ok bros,
    I created an individual project for the signals and slots, and i got the same problem.
    NOTE: it's a Gui application.

    files:
    mainwindow.h | mainwindow.cpp | main.cpp

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. void callSignal();
    17. ~MainWindow();
    18.  
    19. signals:
    20. void _connected(int);
    21.  
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11. callSignal();
    12. bool c = connect(this,SIGNAL(_connected(int)), this, SLOT(close()));
    13. QMessageBox::information(0, "Connect()", QString::number(c)); // shows c = 1 "true"
    14. }
    15.  
    16. void MainWindow::callSignal()
    17. {
    18. emit _connected(1);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    the connect() returns "true" , but the signal isn't working.

    So this simple project has the secret why, i hope you find it.

    Thanx.

  19. #19
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Signal can't be emitted !

    Maybe because you are calling a signal before making a connection ?

  20. #20
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal can't be emitted !

    stampede, i tried to call the signal after the connection, nothing happens.

Similar Threads

  1. Signal isn't emitted from socket
    By 8Observer8 in forum Newbie
    Replies: 3
    Last Post: 14th August 2013, 14:33
  2. loadFinished() signal not emitted
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 0
    Last Post: 27th June 2013, 13:21
  3. QTcpSocket error signal emitted twice
    By CactusPie in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2013, 16:54
  4. signal emitted when I zoom
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 8th July 2009, 18:02
  5. Signal emitted more than once?
    By dbrmik in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2009, 13:44

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.