Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. event loop in Qt dll loaded by MFC App
Forum Updated to NodeBB v4.3 + New Features

event loop in Qt dll loaded by MFC App

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IvanM1976
    wrote on 12 Mar 2015, 07:20 last edited by
    #1

    Hello everybody,

    I have to develop a shared dll that uses serial communication (QSerialPort) with very reatricted time constraints.
    I would like to use readyRead signal in order to process data everytime they arrive.
    the DLL is loaded by an MFC application, so I have no instance of QCoreApplication and I have to create it in the dll.
    How can achieve this?
    I'm trying this way:

    1. creating a global thread and global QCoreApplication, moving the application to thread and executing
    2. creating another thread to move QSerialPort object and connecting readyRead with a slot.

    Doing this the dll works, but i have a lot of problems during the dll unload process (called by MFC FreeLibrary):

    1. if I try to quit the serial port thread, the thread doesn't quit.
    2. sometimes it crashes.

    My question is: what is the correct way to do it?

    thanks

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 12 Mar 2015, 13:07 last edited by JKSH 3 Dec 2015, 13:08
      #2

      Hi,

      I'm not sure if you can safely move a QCoreApplication to another thread. However, I know this works:

      1. Create a new thread using std::thread (NOT a QThread)
      2. In that thread, run a function that constructs the QCoreApplication, and then constructs the QSerialPort, and then call QCoreApplication::exec().
      3. Make sure that you only call QSerialPort methods in that thread.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      I 1 Reply Last reply 13 Mar 2015, 08:34
      1
      • J JKSH
        12 Mar 2015, 13:07

        Hi,

        I'm not sure if you can safely move a QCoreApplication to another thread. However, I know this works:

        1. Create a new thread using std::thread (NOT a QThread)
        2. In that thread, run a function that constructs the QCoreApplication, and then constructs the QSerialPort, and then call QCoreApplication::exec().
        3. Make sure that you only call QSerialPort methods in that thread.
        I Offline
        I Offline
        IvanM1976
        wrote on 13 Mar 2015, 08:34 last edited by
        #3

        @JKSH thank you for the response!!!

        Before reading your solution i founded a similar way to do!

        1. make a global instance of QCoreApplication*
        2. create a wrapper class containing a QThread object and a QSerialPort object
        3. the DLL shares with app 2 functions, one for creation and the other for Release
        4. The create function create the QCoreApplication instance if there isn't already ( check if the app is Qt or not) and create the instance of the wrapper class. The wrapper class create the Qthread, the QSerialPort object and move it to the thread. the wrapper has signal connected to the QSerialPort object slots.
        5. the release function destroy everything.

        the thing i din't though, is that i don't need to call QCoreApplication exec() to have an event loop. what is called here is the QThread::exec() function. I suppose that QCoreApplication in the constructor create just everything needed by the QThread to run and dispatch events!

        1 Reply Last reply
        0

        1/3

        12 Mar 2015, 07:20

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved