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. Signals and Slots with array
Forum Updated to NodeBB v4.3 + New Features

Signals and Slots with array

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 6.2k 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.
  • BeatsteakB Offline
    BeatsteakB Offline
    Beatsteak
    wrote on last edited by
    #1

    Hi,

    I am working with an extra workerthread in Qt which communicates with the mainwindow via Signals and Slots. This works fine so far. Now I want to send a signal containing 2 arrays from my workerthread to the mainwindow, which doesn't really work. Can anyone help me?

    Here is a little extract from my code:

    workerthread.h:

    ...
    private:
       long val1[5];
       int val2[5];
    
    signals:
       void testsignal(long val1[], int val2[]);
    ...
    

    workerthread.cpp:

    ...
    for (i=0;i<5;i++)
    {
       val1[i] = i;
       val2[i] = 6-i;
    }
    emit testsignal(long val1, int val2);
    ...
    

    mainwindow.h:

    ...
    puclic slots:
       testslot(long val1[], int val2[]);
    ...
    

    mainwindow.cpp:

    ...
    MainWindow::MainWindow(QWidget *parent) :
       QMainWindow(parent),
       ui(new Ui::MainWindow)
    {
       ...
       m_workerThread = new WorkerThread();
    
       connect(m_workerThread, SIGNAL(testsignal(long[],int[]), this, SLOT(testslot(long[],int[])));
       ...
    }
    
    ...
    
    void MainWindow::testslot(long val1[], int val2[])
    {
       QString string = QString("Value 1 = %1 ,  Value 2 = %2").arg(val1[0]).arg(val2[0]);
       ui->textBrowser->append(string);
    }
    

    The output in my GUI is:
    Value 1 = 889220 , Value 2 = 889260

    jsulmJ 1 Reply Last reply
    0
    • BeatsteakB Beatsteak

      Hi,

      I am working with an extra workerthread in Qt which communicates with the mainwindow via Signals and Slots. This works fine so far. Now I want to send a signal containing 2 arrays from my workerthread to the mainwindow, which doesn't really work. Can anyone help me?

      Here is a little extract from my code:

      workerthread.h:

      ...
      private:
         long val1[5];
         int val2[5];
      
      signals:
         void testsignal(long val1[], int val2[]);
      ...
      

      workerthread.cpp:

      ...
      for (i=0;i<5;i++)
      {
         val1[i] = i;
         val2[i] = 6-i;
      }
      emit testsignal(long val1, int val2);
      ...
      

      mainwindow.h:

      ...
      puclic slots:
         testslot(long val1[], int val2[]);
      ...
      

      mainwindow.cpp:

      ...
      MainWindow::MainWindow(QWidget *parent) :
         QMainWindow(parent),
         ui(new Ui::MainWindow)
      {
         ...
         m_workerThread = new WorkerThread();
      
         connect(m_workerThread, SIGNAL(testsignal(long[],int[]), this, SLOT(testslot(long[],int[])));
         ...
      }
      
      ...
      
      void MainWindow::testslot(long val1[], int val2[])
      {
         QString string = QString("Value 1 = %1 ,  Value 2 = %2").arg(val1[0]).arg(val2[0]);
         ui->textBrowser->append(string);
      }
      

      The output in my GUI is:
      Value 1 = 889220 , Value 2 = 889260

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Beatsteak You should either use std::array or QVector instead of array. Else in your slot you do not know how many elements the arrays contain.
      And this is wrong:

      emit testsignal(long val1, int val2);
      

      it should be

      emit testsignal(val1, val2);
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • BeatsteakB Offline
        BeatsteakB Offline
        Beatsteak
        wrote on last edited by
        #3

        Thanks! The QVector thing works :)

        1 Reply Last reply
        0

        • Login

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