PDA

View Full Version : Signal Slot not working, I don´t know why



laprast
26th December 2018, 18:00
Hello, I have a problem with the signal slots. I have a main.cpp file and 2 classes, mainwindow and server.

The main.cpp is the next one:



w = new MainWindow();
zerbitzaria = new Server();
//Konexioak egiten dira zerbitzariarekin

qDebug () << QObject::connect(zerbitzaria, &Server::setSEtxekoPuntuakGora, w, &MainWindow::setMWEtxekoPuntuakGora);



I create 2 different objects and the I connect it.

In the server.h file I have this:



class Server: public QObject
{
Q_OBJECT
signals:
void setSEtxekoPuntuakGora();


In server.cpp I call to the signal using emit command:



switch (comandos)
{
case agindua::EtxekoPuntuakGora:
{
emit setSEtxekoPuntuakGora();
break;
}
}


In the mainwindow class, I have defined the slot(mainwindow.h):


public slots:
void setMWEtxekoPuntuakGora();


And then i have implemented (mainwindows.cpp):



void MainWindow::setMWEtxekoPuntuakGora()
{
int A = ui->LCD_Etxekoak->intValue();
A++;
if (A < 999 && A>=0) ui->LCD_Etxekoak->display(A);
}


both classes has the Q_OBJECT. And I have debugged and see that the connect function return true. Finally I put the breakpoint at the emit setSEtxekoPuntuakGora(); point and I see that the debugger stops there. So everything is ok but it never enters in the slot function.

Can anyone help me?
Thank you

ChrisW67
28th December 2018, 23:53
Finally I put the breakpoint at the emit setSEtxekoPuntuakGora(); point and I see that the debugger stops there. So everything is ok but it never enters in the slot function.

How have you determined it never enters the slot function?
Does your code ever return to the Qt event loop?

laprast
29th December 2018, 10:24
How have you determined it never enters the slot function?
Does your code ever return to the Qt event loop?

Thank you for the answer but I don´t understand you.

I put the breakpoint in the emit line, and I have another breakpoint in the slot function. It across the emit setSEtxekoPuntuakGora();, because it stops in the line, but then it doesn´t stops in the other breakpoint at the slot function.

After that all the other functionalities works fine. So I don´t know where is the mistake.

Thank you

d_stranz
29th December 2018, 17:27
w = new MainWindow();
zerbitzaria = new Server();
//Konexioak egiten dira zerbitzariarekin

qDebug () << QObject::connect(zerbitzaria, &Server::setSEtxekoPuntuakGora, w, &MainWindow::setMWEtxekoPuntuakGora);

Where does this code live? In main.cpp or somewhere else? Are you accidentally creating more than one instance of your server?

Putting executable code in a qDebug() statement is a really bad idea, because you don't know what happens in release mode builds. The compiler could simply ignore the whole statement, and then you get no connect() call at all.

Call connect() and assign the result to a variable, then pass that variable to qDebug() for output.

Qt's "event loop" handles all signal / slot calls, all user interactions (key press, mouse click, etc.), and all events generated by the rest of Qt. In order for a Qt application to work, you have to allow control to return to the event loop so that Qt can process the events that have been added since the last time the event loop was executed.

If you write code that does extensive computation or otherwise ties up the program, then control can't return to the event loop, and signals and slots don't get handled. Look at the Qt documentation and read about the event loop.

laprast
30th December 2018, 11:29
Sorry, I found the mistake. i was creating 2 Server instances. When I deleted the first one, everything is working great.

Thank you everybody.

Bye