Important: Please read the Qt Code of Conduct -

How do I start polling with mutex locked thread in QT?

  • Hi,

    I am using Ubuntu 12.04 with QT version 4.8.3.
    In QT main window I manage to open my devices and this part of the code is working.
    Now after I open devices I need to wait if a card is present. It means I have to use polling to get the data from card. But the polling must be infinite loop. Polling for card arrival and removal. Example poll for card arrival every 20ms and when a card is detected I need to switch poll for card removal every 20 ms. So when a card arrival or removal is detected, my app signals QT event such that another QT thread can now proceed to read/write the card.
    I read about QThread, mutex locked so on and I am bit confused.
    I have a main window plus a worker class. In my mainwindow I code as;

    1. Open a reader
      Example: (from my SDK)

    2. If the reader is open

    thread = new QThread();
    worker = new Worker();

    My problem is I don’t know what should I do now. I know I have to use connect for SIGNAL/SLOT and start the card arrival/removal polling. Than if any card detected I have signal to another thread to read from card or write into the card.

    So I don’t know where to start or how to call signal/slot?

    Any help please,
    Kind Regards,

  • "mutex locked..." -- where did you get that. Any thread will do. You don't seem to know signals and slots so read about those first. Just google Qt signals and slots...

    In your thread you need a waiting look that has a delay and a poll to check the card status. You start the loop with the thread's run method. When you for example find the card, just emit signal card_found. If you don't want any signaling back you can just keep sending the status events and just ignore most of them at the other end. To be able to send the signal, you make a class for your thread based on QThread, and the signal is a member of that class. After creating the thread, before running it, you connect the signal to a slot in some of your classes. Mutexes etc. are not needed as you are not passing objects or doing other such stuff. You just send a signal without any data, or perhaps a boolean or integer to communicate the card's presence.

    But depending on things, a thread may be unnecessary complication. You could perhaps just use QTimer() in the same thread to call your polling function.

Log in to reply