[SOLVED] Passing a Struct to a thread



  • Hello,

    I am able to pass different QString etc. based variables to a Thread but are having issues passing a Struct. Below startVCB_1 uses QString and startVCB_2 uses a Struct. I think I am missing something simple. I welcome any feedback?

    This is my simplified mainwindow.h:

    @namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    struct vcbPortConfig {
    QString stringPortName;
    QString stringBaudRate;
    };

    vcbPortConfig int_PortParameters_1;
    
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    

    private slots:
    void startVCB_1();
    void startVCB_2();

    private:
    Ui::MainWindow *ui;
    VirtualCommunicationBus VCBThread_1, VCBThread_2, VCBThread_3, VCBThread_4, VCBThread_5, VCBThread_6;

    };@

    This is my simplified mainwindow.cpp:

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    }

    void MainWindow::startVCB_1()
    {
    VCBThread_1.startVCB_1(ui->int_serialPortInfoListBox_1->currentText(),
    ui->int_baudRateBox_1->currentText(),
    ui->int_dataBitsBox_1->currentText(),
    ui->int_parityBox_1->currentText(),
    ui->int_stopBitsBox_1->currentText(),
    ui->int_flowControlBox_1->currentText());
    }

    void MainWindow::startVCB_1()
    {
    vcbPortConfig int_portParameters_1;
    VCBThread_2.startVCB_2(&int_portParameters_1);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    This is the Qthread object virtualcommunicationbus.h:

    @class VirtualCommunicationBus : public QThread
    {
    Q_OBJECT

    public:
    struct vcbPortConfig {
    QString stringPortName;
    QString stringBaudRate;
    };

    VirtualCommunicationBus(QObject *parent = 0);
    ~VirtualCommunicationBus();
    
    void startVCB_1(const QString &stringPortName, const QString &baudRate, const QString &dataBits, const QString &parity, const QString &stopBits, const QString &flowControl );
    void startVCB_2(const vcbPortConfig &test);
    

    signals:

    private slots:

    private:
    vcbPortConfig test;
    QString stringPortName, baudRate;
    QString dataBits;
    QString parity;
    QString stopBits;
    QString flowControl;
    QMutex mutex;
    bool quit;
    };
    @
    This is the virtualcommunicationbus.cpp :

    @VirtualCommunicationBus::VirtualCommunicationBus(QObject *parent) :
    QThread(parent), waitTimeout(0), quit(false)
    {
    }

    VirtualCommunicationBus::~VirtualCommunicationBus()
    {
    mutex.lock();
    quit = true;
    mutex.unlock();
    wait();
    }

    void VirtualCommunicationBus::startVCB_1(const QString &stringPortName, const QString &baudRate, const QString &dataBits, const QString &parity, const QString &stopBits, const QString &flowControl )
    {
    QMutexLocker locker(&mutex);
    }

    void VirtualCommunicationBus::startVCB_2(const vcbPortConfig &test)
    {
    QMutexLocker locker(&mutex);
    }@

    The issue is that when I compile I get an error 'no mathing function call to 'virtualcommunicationbus::startVCB_2(MainWindow::vcbPortConfig&)'. It works with simply passing a QString and I think I am making a simple mistake with Struct?

    Thank you in advance for your help!


  • Lifetime Qt Champion

    Hi,

    One mistake is that you are declaring the same struct twice in two different classes, so these are two different things (even if the name and the content is the same).
    You should have it in its own header and re-use it (like QString).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.