Signal can't be emitted !
Hello,
file1.h
Code:
{
Q_OBJECT
.... // some code
signals:
void _connection(int);
.... // rest of the code
file1.cpp
Code:
void MyServer::incomingConnection(int handle)
{
MyClient *client = new MyClient(this);
client->setsocket(handle);
emit _connection(handle); // i emit it here
}
file2.h
Code:
#include "file1.h"
{
Q_OBJECT
... // code here
private:
MyServer *server;
... // rest of code
file2.cpp
Code:
server = new MyServer;
connect(server,SIGNAL(_connection()), this, SLOT(connected())); // the slot is declared in the code
The issue:
----------
The signal is not emitted within the incoming connction.
The question:
-------------
Why ?
Thanks,
Vladimir.
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?
1 Attachment(s)
Re: Signal can't be emitted !
there's an incoming connection,
Code:
void MyServer::incomingConnection(int handle)
{
MyClient *client = new MyClient(this);
client->setsocket(handle);
QMessageBox::information(0,
"connected",
"new client");
// this runs, so the function worked. emit _connection(handle); // i emit it here
}
the incomingConnection() is written in italic in the QtCreator. it's already coded.
here's a proof that the code worked:
Attachment 10640
but the signal is never emitted.
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?
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 .
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?
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 !!
Re: Signal can't be emitted !
Quote:
Originally Posted by
Vladimir_
file2.cpp
Code:
server = new MyServer;
connect(server,SIGNAL(_connection()), this, SLOT(connected())); // the slot is declared in the code
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.
Re: Signal can't be emitted !
Quote:
Originally Posted by
Vladimir_
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.
Quote:
the connection istablished well
Does connect() return true? I don't think so.
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 :p
Quote:
Originally Posted by
anda_skoa
The signal is not emitted because it does not exists.
see file1.h and file1.cpp .
what do you mean it does not exist ?
Re: Signal can't be emitted !
1. Is the class with method connected() derived from QObject ?
2. Is the method connected declared as slot ?
Re: Signal can't be emitted !
myclient.h
Code:
{
Q_OBJECT
public:
explicit MyClient
(QObject *parent
= 0);
void setsocket(int Descriptor);
signals:
// can't put signal here, coz [I]incomingConnection()[/I] isn't here.
public slots:
void connected(); // here it is
void disconncted();
void readyRead();
void TaskResult(int number);
void setNum();
//.... rest of the code
myclient.cpp
Code:
void MyClient::connected()
{
}
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.
Re: Signal can't be emitted !
Quote:
Originally Posted by
Vladimir_
Okay guys .. I came up with the solution.
No, you just avoided the mistake you made.
Quote:
Originally Posted by
Vladimir_
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
but you were trying to connect to
You can of course decide to remain ignorant to how Qt's signal/slot feature works and make up some silly rules instead.
Cheers,
_
Re: Signal can't be emitted !
anda_soka, now I called :
Code:
int cn = connect(server,SIGNAL(_connection(int)), this, SLOT(connected()));
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
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).
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
Code:
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QTcpServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include "mainwindow.h"
{
Q_OBJECT
public:
explicit MyServer
(QObject *parent
= 0);
void startServer(int);
protected:
void incomingConnection(int handle);
signals:
void _connection(int handle);
public slots:
private:
MainWindow *main;
};
#endif // MYSERVER_H
myserver.cpp
Code:
#include "myserver.h"
#include "myclient.h"
#include <QtGui>
MyServer
::MyServer(QObject *parent
) :{
main = new MainWindow;
}
void MyServer::startServer(int port)
{
{
QMessageBox::information(0,
"started",
"Server Started!!");
}
else
{
QMessageBox::warning(0,
"Server not started",
"Can't listen on the port "+QString::number(port
))+".<br>" "please change the port.<br>This happened because the port is busy.";
}
}
void MyServer::incomingConnection(int handle)
{
MyClient *client = new MyClient(this);
client->setsocket(handle);
emit _connection(handle);
}
myclient.h
Code:
#ifndef MYCLIENT_H
#define MYCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QThreadPool>
#include <QtGui>
#include "mytask.h"
#include "myserver.h"
#include "mainwindow.h"
{
Q_OBJECT
public:
explicit MyClient
(QObject *parent
= 0);
void setsocket(int Descriptor);
void clientConnected();
signals:
void isConnected();
public slots:
void connected();
void disconncted();
void readyRead();
void TaskResult(int number);
void setNum();
private:
MyClient *client;
MainWindow *main;
MyServer *server;
};
#endif // MYCLIENT_H
myclient.cpp
Code:
#include "myclient.h"
MyClient
::MyClient(QObject *parent
) :{
QThreadPool::globalInstance()->setMaxThreadCount(5);
}
void MyClient::setsocket(int Descriptor)
{
main = new MainWindow;
server = new MyServer;
clientConnected();
int cn = connect(server,SIGNAL(_connection(int)), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
socket->setSocketDescriptor(Descriptor);
}
void MyClient::connected()
{
}
void MyClient::disconncted()
{
qDebug() << "client disconnected!";
// test -= 1;
}
void MyClient::clientConnected()
{
if(handshake == "newClient")
{
emit isConnected();
}
}
void MyClient::readyRead()
{
// time consumer
MyTask *mytask = new MyTask();
mytask->setAutoDelete(true);
connect(mytask, SIGNAL(Result(int)), this, SLOT(TaskResult(int)), Qt::QueuedConnection);
QThreadPool::globalInstance()->start(mytask);
}
void MyClient::TaskResult(int number)
{
Buffer.append("Task Result = ");
Buffer.
append(QString::number(number
));
socket->write(Buffer);
}
void MyClient::setNum()
{
emit isConnected();
}
Re: Signal can't be emitted !
Quote:
Originally Posted by
Vladimir_
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.
Quote:
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.
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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
void callSignal();
~MainWindow();
signals:
void _connected(int);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
callSignal();
bool c = connect(this,SIGNAL(_connected(int)), this, SLOT(close()));
}
void MainWindow::callSignal()
{
emit _connected(1);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
the connect() returns "true" , but the signal isn't working.
So this simple project has the secret why, i hope you find it.
Thanx.
Re: Signal can't be emitted !
Maybe because you are calling a signal before making a connection ?
Re: Signal can't be emitted !
stampede, i tried to call the signal after the connection, nothing happens.