Results 1 to 13 of 13

Thread: one signal to two slots for reading and logging in a file

  1. #1
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default one signal to two slots for reading and logging in a file

    hi
    I want to read from serial port (rs232) then processing and in the same time saving in a file. one way is using QThread but could i use "readyRead" signal with two slots like this instead of QTread?

    signal(readyRead) , slot(doing some things and processing)

    signal(readyRead) , slot(saving_file())

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: one signal to two slots for reading and logging in a file

    I don't know what threads have to do with the question.
    Any way, have a look at the signal/slot documentation.
    If you do, you will see you can connect any signal to any number of slots.
    So yes, you can connect your readyRead() signal to two different slots.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: one signal to two slots for reading and logging in a file

    But if in the first slot you do readAll() on the serial port then in the second you will not read anything from the port.

  4. The following user says thank you to Lesiok for this useful post:

    QJak (16th April 2018)

  5. #4
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: one signal to two slots for reading and logging in a file

    thanks. i need to read in two slots. but in first slot after reading, i do some processing and in second slot after reading i need to put it into a file.
    if i understand your answer, you mean is that second slot can not read?

  6. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: one signal to two slots for reading and logging in a file

    Yes. Why not to use one slot and simple call logging method from slot ?

  7. The following user says thank you to Lesiok for this useful post:

    QJak (16th April 2018)

  8. #6
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: one signal to two slots for reading and logging in a file

    realy thanks. please help me to do this. how can i use a slot and call saving into a file?

  9. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: one signal to two slots for reading and logging in a file

    Slot is normal class method. So in body call some another method with QByteArray argument to save data to file.
    These are the basics of C++.

  10. The following user says thank you to Lesiok for this useful post:

    QJak (16th April 2018)

  11. #8
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: one signal to two slots for reading and logging in a file

    thanks again. you mean i call saving function from body of slot? (i need to do two works (1-reading and processing 2-reading and writing in a file) together like tread.
    please explain more

  12. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: one signal to two slots for reading and logging in a file

    Yes. In slot You need :

    1. Read data from port.
    2. Process data.
    3. Write data in a file.

    Steps 2 and 3 can be swapped.

  13. #10
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: one signal to two slots for reading and logging in a file

    thanks but processing and writing after that is too slow. i need to do 2 jobs in the same time
    is this work correct to emit a signal after reading like this:
    i define a signal in name of : signal_saving(QByteArray) and connect this signal to the second slot below
    then define 2 slots: slot1_readingAndProcessing() , slot2_saving(QByteArray)
    after connecting signal_saving(QByteArray) to slot2_saving(QByteArray) i emit this signal after reading data in body of first slot. imean:
    slot1_readingAndProcessing()
    {
    reading data and put in QByteArray
    emit signal_saving(QBytArray)
    then processing
    }


    is this correct?

  14. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: one signal to two slots for reading and logging in a file

    Yes, this is correct under one condition : connection to slot2_saving is in Qt::QueuedConnection. In Qt:irectConnection emiting a signal is equivalent to calling the method. Read documentation.

  15. The following user says thank you to Lesiok for this useful post:

    QJak (16th April 2018)

  16. #12
    Join Date
    Dec 2016
    Posts
    30
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: one signal to two slots for reading and logging in a file

    this way has not my desier result (working paralel). please help me

  17. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: one signal to two slots for reading and logging in a file

    ...thanks but processing and writing after that is too slow. i need to do 2 jobs in the same time...

    ...working paralel...

    please help me
    Please explain why you insist this must be a parallel operation (you say so here and in your other posts). What is your "processing" that makes it so slow? And writing a small amount of data to a log file should be very fast, unless you are opening and closing the file for every write.

    Based on what you are asking and how you are asking it, I am not sure you understand enough to write a multi-threaded application that will work correctly.
    <=== 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.

Similar Threads

  1. Replies: 29
    Last Post: 24th January 2017, 13:30
  2. Replies: 0
    Last Post: 20th July 2013, 04:07
  3. Signal/Slots, Where can i use it?
    By Tio in forum Newbie
    Replies: 2
    Last Post: 25th May 2010, 02:36
  4. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 04:50
  5. Logging to a file and using same model for diff views
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 11th May 2007, 00:16

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.