+ Reply to Thread
Results 1 to 11 of 11

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

  1. #1
    Join Date
    Dec 2016
    Posts
    14
    Thanks
    5
    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,688
    Thanks
    21
    Thanked 415 Times in 408 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,362
    Thanked 251 Times in 246 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
    14
    Thanks
    5
    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,362
    Thanked 251 Times in 246 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
    14
    Thanks
    5
    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,362
    Thanked 251 Times in 246 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
    14
    Thanks
    5
    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,362
    Thanked 251 Times in 246 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
    14
    Thanks
    5
    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,362
    Thanked 251 Times in 246 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)

+ Reply to Thread

Similar Threads

  1. Replies: 29
    Last Post: 24th January 2017, 12:30
  2. Replies: 0
    Last Post: 20th July 2013, 03:07
  3. Signal/Slots, Where can i use it?
    By Tio in forum Newbie
    Replies: 2
    Last Post: 25th May 2010, 01:36
  4. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 03:50
  5. Logging to a file and using same model for diff views
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 10th May 2007, 23: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