Results 1 to 8 of 8

Thread: God! QSocketNotifier emit the activated signal endlessly

  1. #1
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default God! QSocketNotifier emit the activated signal endlessly

    please anyone here help me to solve this problem.

    I am working on linux 2.4.19 (arm based) + qt-embedded-free-3.3.8b.
    and now I am writing a custom keyboard handler which subclass from QWSKeyBoardHandler.

    Qt Code:
    1. class MyKbdHandler: public QObject, public QWSKeyBoardHandler {
    2. Q_OBJECT
    3. public:
    4. MyKbdHandler();
    5. ~MyKbdHandler();
    6.  
    7. private slots:
    8. void readKbdData();
    9.  
    10. private:
    11. QSocketNotifier *m_kbdNotifier;
    12. int m_kbdFd;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MyKbdHandler::MyKbdHandler()
    2. : QObject(), QWSKeyBoardHandler()
    3. {
    4. m_kbdNotifier = NULL;
    5. m_kbdFd = ::open("/dev/input", O_RDONLY|O_NONBLOCK);
    6. if (m_kbdFd < 0) {
    7. qDebug("Can not open input device!\n");
    8. return;
    9. }
    10.  
    11. m_kbdNotifier = new QSocketNotifier(m_kbdFd, QSocketNotifier::Read, this);
    12. connect(m_kbdNotifier, SIGNAL(activated(int)), this, SLOT(readKbdData()));
    13. }
    14.  
    15. MyKbdHandler::~MyKbdHandler()
    16. {
    17. if (m_kbdFd >= 0) {
    18. ::close(m_kbdFd);
    19. m_kbdFd = -1;
    20. }
    21.  
    22. if (m_kbdNotifier != NULL) {
    23. delete m_kbdNotifier;
    24. m_kbdNotifier = NULL;
    25. }
    26. }
    27.  
    28. MyKbdHandler::readKbdData()
    29. {
    30.  
    31. char buf[1024] = { 0 };
    32. int n = ::read(m_kbdFd, buf, sizeof(buf));
    33. qDebug("MyKbdHandler::readKbdData = %d\n", n);
    34.  
    35. // do key code mapping and call processKeyEvent here ...
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 

    after reading all of the available data(the read call already return 0), the activated() signal emit over and over again, and readKbdData() also get invoked. the cpu reached to 98%.!

    any idea ?

  2. #2
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default Re: God! QSocketNotifier emit the activated signal endlessly

    any idea???

  3. #3
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: God! QSocketNotifier emit the activated signal endlessly

    I just had the same problem, when using QSocketNotifier to a Named Pipe / Fifo (Linux).

    The activated() signal is not endlessly, but veeeery often until you read all data from the pipe.

    Calling connect( .... , Qt::BlockingQueuedConnection ) worked fine for me. There is the code showing the full test case:

    Qt Code:
    1. class ReceiverThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ReceiverThread()//(QObject * parent = 0):QThread(parent)
    7. {
    8. rxNotifier=0;
    9. }
    10. ~ReceiverThread() {
    11. close(rxfd);
    12. }
    13.  
    14. private:
    15. double sampleTime;
    16. int rxfd, k;
    17. QSocketNotifier *rxNotifier;
    18.  
    19. protected:
    20. virtual void run() {
    21. rxfd = open("/tmp/testfifo", O_RDONLY);
    22. if (rxfd==-1) {qDebug()<<"\nError opening fifo"; }
    23. fcntl(rxfd, F_SETFL, fcntl(rxfd, F_GETFL) | O_NONBLOCK);
    24. startRxNotifier();
    25. exec();
    26. }
    27.  
    28. void startRxNotifier() {
    29. if(rxfd >= 0) {
    30. if(rxNotifier == 0) {
    31. rxNotifier = new QSocketNotifier(rxfd, QSocketNotifier::Read);//, this);
    32. connect( rxNotifier, SIGNAL(activated(int)), SLOT(rxEvent(int)), Qt::BlockingQueuedConnection );
    33.  
    34. } else {
    35. // Debug("QSocketNotifier re-enabled!")
    36. rxNotifier->setEnabled(true);
    37. }
    38. }
    39. }
    40.  
    41. public slots:
    42. void rxEvent(int socket) {
    43. int readret;
    44. int buf[1024];
    45. qDebug()<<"rxEvent()";
    46. readret = read(socket/*rxfd*/, buf, 2*sizeof(double));
    47. qDebug()<<"Received:"<<buf[0]<<", "<<buf[1]<<" read() returns "<<readret;
    48. }
    49. };
    To copy to clipboard, switch view to plain text mode 

    Anyway, this behaviour is not described in the current documentation...

    Cheers,
    Joerg

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

    Default Re: God! QSocketNotifier emit the activated signal endlessly

    I had a similar issue. What I remember working (at least AFAIR) was to disable buffering for the file descriptor on the system level before feeding the descriptor to the socket notifier.

  5. #5
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: God! QSocketNotifier emit the activated signal endlessly

    Ok, I learned now, that the problem has to do with the thread usage in my case: I learned, that the SLOT(rxEvent()) is processed in the event loop of the core application whilst the SIGNAL(activated) is emitted in the thread. This is solved by changing the thread affinitiy of the slot by calling moveToThisThread() in the core application.
    Qt Code:
    1. ReceiverThread *receiverThread = new ReceiverThread();
    2. app.connect(receiverThread, SIGNAL(finished()), &app, SLOT(quit()));
    3. receiverThread->moveToThread(receiverThread);
    4. receiverThread->start();
    To copy to clipboard, switch view to plain text mode 

    So using Qt::BlockingQueuedConnection is a bad idea in this case.

    The doc of moveToThisThread() provides a god explaination.

    Cheers

  6. #6
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: God! QSocketNotifier emit the activated signal endlessly

    I tto face same issue where slot is called continuously even when i run in different thread.How do i overcome this ? Can anybody suggest me a proper answer to close this ASAP


    Regards
    Bala B

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

    Default Re: God! QSocketNotifier emit the activated signal endlessly

    Did you try code from the previous post?
    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.


  8. #8
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: God! QSocketNotifier emit the activated signal endlessly

    yeah..but still i get the activated signal continuously in that thread as well

Similar Threads

  1. emit qt signal is very slow how it can be optimized?
    By sawerset in forum Qt Programming
    Replies: 8
    Last Post: 30th December 2008, 09:21
  2. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  3. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  4. how to know which button emit the signal?
    By coder1985 in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2008, 14:26
  5. emit the activated signal on a combobox
    By Equilibrium in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2007, 12:33

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.