[SOLVED] Monitoring an object changed asynchronously by a Callback function



  • I have a C style callback function for DDE communication, called asynchronously.
    It works as it should.

    Now I want this function to have a QBuffer or QLinkedList or at least an integer iterator
    that should get a new value each time this callback function is executed.

    I want to monitor the change of QBuffer from within my main function.

    I could use void QIODevice::bytesWritten(qint64 bytes), but Signals and Slots are said to be ten times slower than callbacks.

    Or shall I just implement a loop in my main function like this:

    while (true)
    {
      if myBuffer.changed()
         doSomething();
    
      QThread::msleep(SomeMilliseconds);
    }
    

    BTW, shall myBuffer be a static variable?


  • Lifetime Qt Champion

    Hi,

    If you need that much performance, you'd better do a benchmark. What is your use case ?



  • @SGaist
    I was told today that we'll use milliseconds interval.
    So it is no longer a performance question.

    I just implemented a QTimer for monitoring the callback function changing my vector,
    and it was fast enough.


  • Lifetime Qt Champion

    Why not embed that QVector in a QObject wrapper that will emit a signal whenever the vector is modified ?



  • @SGaist said:

    Why not embed that QVector in a QObject wrapper that will emit a signal whenever the vector is modified ?

    QVector in a QObject wrapper?

    I thought about something like:

    QBuffer buffer(&byteArray);
    

    and then use the above mentioned QBuffer signals.


  • Lifetime Qt Champion

    That's also an alternative yes, you can even go further using a QIODevice derived class and create a device that would your data directly



  • @SGaist

    Yes, I subclassed QBuffer, implemented my own

    qint64 MyBuffer::writeData(const char *data, qint64 len)
    {
        buffer().clear();
        buffer().append(data,len);
        emit bytesWritten(len);
        return len;
    }
    

    and then:

    QObject::connect(DDEComm::instance()->buf,&QBuffer::bytesWritten,[=](qint64 bytes)
    {
        printf("bytesWritten: %d, Buffer: %s\n", (int) bytes, DDEComm::instance()->buf->data().data());
    });
    

    It works so far as it should.

    But what was your proposal about embedding QVector in a QObject wrapper?
    Is is something where one has to use http://doc.qt.io/qt-5/properties.html ?

    Anyway I'd mark then this thread as SOLVED.


  • Lifetime Qt Champion

    No you don't have to. The use of properties depends on your software architecture and your class design.



  • @SGaist
    How else can I inroduce my own SIGNAL function for any Object?


  • Lifetime Qt Champion

    Just declare your signal in the class header and emit it in your code. Take for example QTimer, the timeout signal is not attached to any property and none of them has any signal.



  • @SGaist

    Thanks. Now I have a more clear picture about signals.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.