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]Can't connect signal to slot with QVector arguments
QtWS25 Last Chance

[SOLVED]Can't connect signal to slot with QVector arguments

Scheduled Pinned Locked Moved General and Desktop
qt 5.4.0connectsignal & slotqt creatorthreads
6 Posts 2 Posters 8.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.
  • P Offline
    P Offline
    Preston_H
    wrote on last edited by Preston_H
    #1

    Hello forum,

    I am trying to update a UI element on the main thread from a separate thread that's been initiated. I've read that to do so, one must have the object in the secondary thread emit a signal to a receiver on the main thread. The trouble is that the signal and slot should both have two arguments, both of which are QVectors (or references to QVectors, ideally), and my compiler does not like this.

    Here is what I've tried doing:

    class A: public QObject
    {
       Q_Object
       public:
         A();
         void EmitRequest() {emit RequestUpdate(x_, y_);};
         QVector<double> x_,  y_;
       public signals:
         void RequestUpdate(QVector<double> x, QVector<double> y);
    }   
    
    class B: public A
    {
       public:
         B();
    }
    
    class C
    {
      public slots:
         void ProcessUpdateRequest(QVector<double> x, QVector<double> y){//do something to UI element};
    }     
    
    
    B b;
    C c;
    
    connect(&b, SIGNAL(RequestUpdate(QVector<double>, QVector<double>)), &c, SLOT(ProcessUpdateRequest(QVector<double>, QVector<double>)));
    

    Here is the compiler error:

    ... error: no matching function for call to 'MainWindow::connect(Protocol*&, const char*, Plot*&, const char*)'
    connect(protocolb, SIGNAL(RequestUpdatePlot(QVector<double>, QVector<double>)), plotb, SLOT(UpdatePlot(QVector<double>, QVector<double>)));

    I've tried looking up elsewhere on line how to connect signals to slots when there are arguments in the calls. I tried changing the connect call to the new style offered in Qt 5.4:

    connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);

    I've also tried using qRegisterMetaType<T>() for QVectors as some people suggested, but that didn't work either.

    Any ideas?

    Thanks!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      At least your sample code does not inherit from QObject and the Q_OBJECT macro is missing. Not sure if it is possible to use "signal" as a signal as given in your example code.
      Your sample code does obviously not match error message. Therefore one needs a crystal ball for finding more.

      Vote the answer(s) that helped you to solve your issue(s)

      P 1 Reply Last reply
      1
      • K koahnig

        At least your sample code does not inherit from QObject and the Q_OBJECT macro is missing. Not sure if it is possible to use "signal" as a signal as given in your example code.
        Your sample code does obviously not match error message. Therefore one needs a crystal ball for finding more.

        P Offline
        P Offline
        Preston_H
        wrote on last edited by
        #3

        @koahnig
        Would it be easier to post the code verbatim? I usually try and post only the minimal amount of code needed to convey what I'm trying to do so it's faster to parse. You're right though, I forgot some necessary parts in the example code. Those parts are in the real code, and I've added them to the example code and expanded it some.

        K 1 Reply Last reply
        0
        • P Preston_H

          @koahnig
          Would it be easier to post the code verbatim? I usually try and post only the minimal amount of code needed to convey what I'm trying to do so it's faster to parse. You're right though, I forgot some necessary parts in the example code. Those parts are in the real code, and I've added them to the example code and expanded it some.

          K Offline
          K Offline
          koahnig
          wrote on last edited by koahnig
          #4

          @Preston_H
          It depends. Basically some shortened code should be good enough. The problem was that you may have removed the key parts.

          What is the length of those vectors?
          Possibly it could be better to use pointers to the vectors rather than the full vector.
          I would not expect that you have to register the vectors because they are part of Qt libs.

          Owh, class 'c' does not inherit from QObject. Check if you simply forgot in the post.

          [edit, koahnig]

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          1
          • P Offline
            P Offline
            Preston_H
            wrote on last edited by
            #5

            Owh, class 'c' does not inherit from QObject. Check if you simply forgot in the post.

            Yep, that was it... Next time I'll force myself to go to sleep and look at the code in the morning rather than obsess over it when I'm tired.

            I would not expect that you have to register the vectors because they are part of Qt libs.

            Interestingly, I still have to register QVector<double> as a meta type. The code doesn't work without that (although, it does give an informative error message when the line is missing).

            Thanks for your help!

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              I would have expected that all Qt type are already registered. However, this is a template which you registered with template argument. That is presumably the reason. A template delivers infinite possibilities. I did not know.

              Vote the answer(s) that helped you to solve your issue(s)

              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