Results 1 to 4 of 4

Thread: QSocketNotifier Qt 4.5 Windows

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QSocketNotifier Qt 4.5 Windows

    Hey trolls,
    Consider the following code:

    Qt Code:
    1. socketNotifier = new QSocketNotifier(0, QSocketNotifier::Read, q);
    2. socketNotifier->setEnabled(true);
    3.  
    4. QObject::connect(socketNotifier, SIGNAL(activated(int)), q, SLOT(onReadyRead()));
    5.  
    6. QFile file;
    7. if (file.open(0, QFile::WriteOnly) == true)
    8. {
    9. QTextStream stream(&file);
    10. stream << "lol";
    11. file.close();
    12. }
    To copy to clipboard, switch view to plain text mode 

    My onReadyRead() slot never gets called.

    Why ?

  2. #2
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QSocketNotifier Qt 4.5 Windows

    http://lists.trolltech.com/qt-intere...ad01097-0.html

    stdin on Windows is a magic little beast that tries to work both like a
    windows handle, and as the Posix FILE* handle (e.g., fileno(stdin) returns
    something that can be passed to other BSD emulation functions). The problem
    is that it is not a pipe. It isn't a socket. It's not a file. It is a
    special buffered device that is flushed on endline or EOF, and sometimes it
    can get flushed by other activity.

    You cannot use QSocketNotifier to receive notifications on stdin, because
    Windows doesn't send single object notifications on it. QSocketNotifier on
    Windows is designed to work with network sockets, and stdin isn't a socket
    per se.

    Andreas

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QSocketNotifier Qt 4.5 Windows

    I'm now using a QTimer, but I get a freeze in my callback:

    Qt Code:
    1. d->timer.stop();
    2.  
    3. QFile file;
    4. file.open(stdin, QFile::ReadOnly);
    5. if (file.isReadable())
    6. {
    7. file.readAll(); // Freezes the process
    8. }
    9.  
    10. d->timer.start(100);
    To copy to clipboard, switch view to plain text mode 

    Any idea?

  4. #4
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QSocketNotifier Qt 4.5 Windows

    Here is the solution:

    Qt Code:
    1. #include "qkReceptor.h"
    2.  
    3. // Qt includes
    4. #include <qsocketnotifier>
    5. #include <qtextstream>
    6. #include <qtimer>
    7.  
    8. //=============================================================================
    9. // Private
    10. //=============================================================================
    11.  
    12. #include "qkReceptor_p.h"
    13.  
    14. qkReceptorPrivate::qkReceptorPrivate(qkReceptor * p)
    15. : qkPrivate(p)
    16. {
    17. }
    18.  
    19. void qkReceptorPrivate::init()
    20. {
    21. Q_Q(qkReceptor);
    22.  
    23. q->start();
    24. }
    25.  
    26. //=============================================================================
    27. // Ctor / dtor
    28. //=============================================================================
    29.  
    30. qkReceptor::qkReceptor(QObject * parent)
    31. : QThread(parent), qkPrivatable(new qkReceptorPrivate(this))
    32. {
    33. Q_D(qkReceptor);
    34. d->init();
    35. }
    36.  
    37. qkReceptor::~qkReceptor()
    38. {
    39. }
    40.  
    41. //=============================================================================
    42. // Protected QThread reimplementation
    43. //=============================================================================
    44.  
    45. void qkReceptor::run()
    46. {
    47. // A timer to avoid high CPU charge
    48. QTimer timer;
    49.  
    50. connect(&timer, SIGNAL(timeout()), this, SLOT(onCheckStdin()), Qt::DirectConnection);
    51.  
    52. timer.start(100);
    53.  
    54. QThread::exec();
    55. }
    56.  
    57. //=============================================================================
    58. // Private slots
    59. //=============================================================================
    60.  
    61. void qkReceptor::onCheckStdin()
    62. {
    63. QTextStream stream(stdin);
    64.  
    65. // Do we have a new line to be read ?
    66. QString line = stream.readLine();
    67.  
    68. if (line != "")
    69. {
    70. // Do something with our line
    71.  
    72. qkPrint(line.C_STR);
    73. }
    74. }
    To copy to clipboard, switch view to plain text mode 

    If someone finds a proper way on windows, keep us posted.
    Last edited by bunjee; 18th April 2009 at 21:39.

Similar Threads

  1. Qt Jambi, deploying app on Mac & Windows fails
    By ChrisColon in forum Installation and Deployment
    Replies: 2
    Last Post: 16th February 2009, 22:05
  2. Replies: 5
    Last Post: 15th January 2009, 09:03
  3. Opening text file fails after autostartup on windows
    By yogourta in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2008, 03:52
  4. Windows not appearing in XP.
    By beardybloke in forum Qt Programming
    Replies: 7
    Last Post: 24th October 2007, 17:32
  5. converting unix exe to windows binary
    By deekayt in forum General Programming
    Replies: 2
    Last Post: 17th September 2006, 01:00

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.