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. [SOLVED] Passing a Struct to a thread
Forum Update on Monday, May 27th 2025

[SOLVED] Passing a Struct to a thread

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.5k Views
  • 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.
  • V Offline
    V Offline
    Vito3223
    wrote on 20 Sept 2013, 18:48 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Sept 2013, 20:55 last edited by
      #2

      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).

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      20 Sept 2013, 18:48

      • Login

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