Results 1 to 4 of 4

Thread: QBuffer bytesWritten() problem

  1. #1
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QBuffer bytesWritten() problem

    Hi all,
    I spent some time trying to see the signal bytesWritten working over a QBuffer instance, but without any succes... I post here a little example of my code.

    Qt Code:
    1. QBuffer buffer;
    2. char ch;
    3.  
    4. connect(&buffer,SIGNAL(bytesWritten(qint64)),this,SLOT(test(qint64)));
    5.  
    6. buffer.open(QBuffer::ReadWrite);
    7. buffer.write("Qt rocks!");
    8. buffer.seek(0);
    9. buffer.getChar(&ch); // ch == 'Q'
    10. qDebug("%c",ch);
    11. buffer.getChar(&ch); // ch == 't'
    12. qDebug("%c",ch);
    13. buffer.getChar(&ch); // ch == ' '
    14. qDebug("%c",ch);
    15. buffer.getChar(&ch); // ch == 'r'
    16. qDebug("%c",ch);
    To copy to clipboard, switch view to plain text mode 

    this is the test SLOT:

    Qt Code:
    1. void classname::test()
    2. {
    3. qDebug("test test test");
    4. }
    To copy to clipboard, switch view to plain text mode 

    The code compile without any problem, but the signal bytesWritten is always absent!!!
    I hope someone can help me, because I'm stuck on this annoying problem.

    In my opinion could be a Qt bug, but since I'm a novice in Qt4 programming, I like to discuss with someone expert (or guru... ) before thinking bad things!!!

    A possible workaround is to define a new QIODevice subclass in which I have to reimplement the bytesWritten signal in order to create a new QBuffer working subclass!
    But I hope to make the original one working!

    Best regards,
    Alessandro

  2. #2
    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: QBuffer bytesWritten() problem

    It's not nice to say "hey, something doesn't work in my code, I think it's a Qt bug". Qt is open source, read the source of QBuffer write and see when the signal gets emitted.
    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.


  3. #3
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: QBuffer bytesWritten() problem

    Thanks for the reply!
    I apologize for my presumption... you are right!
    But I told so, since a friend of mine has the same problem.
    We found yesterday a possible workaround of this problem... It seems that connecting the two signals bytesWritten and readyRead, the signal bytesWritten is emitted:

    Qt Code:
    1. socket = new QBuffer(&pckBuffer,this);
    2. connect(socket,SIGNAL(bytesWritten(qint64)),pCl,SLOT(SendPck(qint64)));
    3. connect(socket,SIGNAL(readyRead()),this,SLOT(dummy()));
    To copy to clipboard, switch view to plain text mode 

    where dummy can be also a slot that does nothing, just like this:

    Qt Code:
    1. void classname::dummy()
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 

    With this configuration, the code works for me and also for my friend.
    We don't know exactly why, but the code really works!!! Maybe we will check the reason in the Qt source, as suggested by you!

    Best Regards,
    Alessandro

  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: QBuffer bytesWritten() problem

    I somehow doubt connecting a slot to another signal will start making this signal work.

    If you had looked into Qt's source code you would have noticed a construction like this:
    Qt Code:
    1. #ifndef QT_NO_QOBJECT
    2. d->writtenSinceLastEmit += len;
    3. if (d->signalConnectionCount && !d->signalsEmitted && !signalsBlocked()) {
    4. d->signalsEmitted = true;
    5. QMetaObject::invokeMethod(this, "_q_emitSignals", Qt::QueuedConnection);
    6. }
    7. #endif
    To copy to clipboard, switch view to plain text mode 

    From this it's obvious you need to return to the event loop for the signal to be emitted. Other signals and slots have nothing to do with this. Also if you took a look at _q_emitSignals() you'd notice that bytesWritten() and readyRead() are always emitted together regardless of actual read and write operations (which makes sense because if you write something to the buffer it can be read back from the buffer immediately). Now inspect your code and determine what is going on there instead of using silly workarounds.
    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.


Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.