Qt World Summit: Submit your Presentation

Few emits in one slot

  • Hello,
    I don't understand one thing.I must use in aplication comunication with modbus and comparation of values in varables. In case of conditions "happens" events.For now I'm using thread for modbus comunication. Now I'm testing a drawt of main function.
    My problem is : I would like to send at least two emits wchich runs functions on modbus thread. Additionally main function is cyclic called by timer (I want to have fast refersh of values from modbus)
    Now part of code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "settings.h"
    #include <QStatusBar>
    #include <QDebug>
    #include <QSqlQueryModel>
    #include <QFile>
    #include <QThread>
    #include "modbus.h"
    MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),
        ui(new Ui::MainWindow)
        timer_1=new QTimer(this);
        QThread* thread = new QThread;
        modbus* Modbus = new modbus;
        connect(this, SIGNAL (cyl_1_FF()), Modbus, SLOT (Cylinder_1_FF()));
        connect(this, SIGNAL(Read_1()),Modbus, SLOT(Read_1_value()));
    void MainWindow::Test_mode_1(){
    //Main function
    emit Read_1();
    emit cyl_1_FF();

    Problem is that: when is only one "emit" it's working. When are two "emits" - no one works.
    I've tried also connetion type" Qt::BlockingQueuedConnection" but also not works.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Before starting to add threading to your application, you should check whether the asynchronous nature of Qt is not already enough to manage the data coming from modbus.

    Then why do you need to have two different functions called one after the other ? Are you sure it's the correct design ?

  • Hello,

    Then why do you need to have two different functions called one after the other ? Are you sure it's the correct design ?

    This is an example for begin. Normally should be:

    emit Read_1();
    if( something) {
    emit cyl_1_FF();
    //other code
    emit cyl_1_RW();
    //other code

    I'm testing modbus communication using QSerialPort. I've heard when using separatelly thread that thread collect all "thinks to do" on stack and execute one by one. But I see that is not true. Those two signals are emited, but almost in the same time. This makes mess on QSerialPort. I must use some comunication switch.

  • Lifetime Qt Champion

    Well, your example shows that you seem to want to have a sequential blocking behaviour while using an asynchronous system. That doesn't seem right.

    By the way, why not use the QSerialBus module which support modbus ?

  • @Narki1 said in Few emits in one slot:

    no one works.

    What does this means? it crashes? How are you debugging the problem?

Log in to reply