Results 1 to 5 of 5

Thread: Signal Slot not working, I don´t know why

  1. #1
    Join Date
    Dec 2018
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Signal Slot not working, I don´t know why

    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:

    Qt Code:
    1. w = new MainWindow();
    2. zerbitzaria = new Server();
    3. //Konexioak egiten dira zerbitzariarekin
    4.  
    5. qDebug () << QObject::connect(zerbitzaria, &Server::setSEtxekoPuntuakGora, w, &MainWindow::setMWEtxekoPuntuakGora);
    To copy to clipboard, switch view to plain text mode 

    I create 2 different objects and the I connect it.

    In the server.h file I have this:

    Qt Code:
    1. class Server: public QObject
    2. {
    3. Q_OBJECT
    4. signals:
    5. void setSEtxekoPuntuakGora();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. switch (comandos)
    2. {
    3. case agindua::EtxekoPuntuakGora:
    4. {
    5. emit setSEtxekoPuntuakGora();
    6. break;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    In the mainwindow class, I have defined the slot(mainwindow.h):
    Qt Code:
    1. public slots:
    2. void setMWEtxekoPuntuakGora();
    To copy to clipboard, switch view to plain text mode 

    And then i have implemented (mainwindows.cpp):

    Qt Code:
    1. void MainWindow::setMWEtxekoPuntuakGora()
    2. {
    3. int A = ui->LCD_Etxekoak->intValue();
    4. A++;
    5. if (A < 999 && A>=0) ui->LCD_Etxekoak->display(A);
    6. }
    To copy to clipboard, switch view to plain text mode 

    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

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

    Default Re: Signal Slot not working, I don´t know why

    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?

  3. #3
    Join Date
    Dec 2018
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signal Slot not working, I don´t know why

    Quote Originally Posted by ChrisW67 View Post
    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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,195
    Thanks
    237
    Thanked 668 Times in 658 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Signal Slot not working, I don´t know why

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Dec 2018
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signal Slot not working, I don´t know why

    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

Similar Threads

  1. Replies: 5
    Last Post: 27th July 2015, 10:44
  2. Signal and Slot connection not working !!
    By prakash437 in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2010, 11:16
  3. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 23:55
  4. QObject signal/slot not working
    By Msnforum in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2009, 23:50
  5. Signal Slot not working
    By munna in forum Qt Programming
    Replies: 8
    Last Post: 6th November 2006, 11:36

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.