Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to make a template?
QtWS25 Last Chance

How to make a template?

Scheduled Pinned Locked Moved Solved Qt 6
11 Posts 4 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.
  • C Offline
    C Offline
    Christina123
    wrote on last edited by
    #1

    Hi, I want to create a template that can be used to emit different types of data but with same signal name. For example, dataReady(T data); is my signal and I wish this signal can emit data that is of type int, bytearray and some user-defined type. I am wondering how can I achieve this?

    JonBJ kshegunovK 2 Replies Last reply
    0
    • C Christina123

      Hi, thank you for replying to my answers. The problem that I encounter is that when I try to overload the a signal function, an error occurs saying error: no matching member function for call to 'connect'. My code is below:

      signals:
          void sendData(MyPath data); \\ user-defined data type
          void sendData(QSize data);
      
      connect(rthread, &ReceiverThread::sendData, this, &Receiver::readData);
      

      I will very much want to use QVariant for my code, but unfortunately, I have a user-defined type which is vector<QPainterPath> and this is not supported by QVariant. So, QVariant may not be a feasible way.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #6

      @Christina123 said in How to make a template?:

      The problem that I encounter is that when I try to overload the a signal function, an error occurs saying error: no matching member function for call to 'connect'.

      Yes, that's expected. How should the compiler know which of the two overloads it's supposed to pick? You have to guide it:

      QObject::connect(rthread, QOverload<TypesOfTheArguments>::of(&ReceiverThread::sendData), this, &Receiver::readData);
      

      I will very much want to use QVariant for my code, but unfortunately, I have a user-defined type which is vector<QPainterPath> and this is not supported by QVariant. So, QVariant may not be a feasible way.

      Custom types can be put in a variant, but it's involved (somewhat). They need to be copyable and constructible, and also they need to be declared as metatypes.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • C Christina123

        Hi, I want to create a template that can be used to emit different types of data but with same signal name. For example, dataReady(T data); is my signal and I wish this signal can emit data that is of type int, bytearray and some user-defined type. I am wondering how can I achieve this?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Christina123
        While you wait for a better C++-er than I to answer about the template approach....

        ...Have you considered whether one signal with a QVariant argument would suit your situation?

        Nothing wrong with dedicated-type-templates approach, just which would you prefer?

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #3

          Moc doesn't understand templates.

          Verdigris does, and might work for this use case. The macros is uses are different.
          Moc-ng does as well, and I think is supposed to be source-compatible with moc.

          I haven't used either one.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • C Christina123

            Hi, I want to create a template that can be used to emit different types of data but with same signal name. For example, dataReady(T data); is my signal and I wish this signal can emit data that is of type int, bytearray and some user-defined type. I am wondering how can I achieve this?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #4

            @Christina123 said in How to make a template?:

            I am wondering how can I achieve this?

            If the class is a template, then it's a different class for each one instantiation (i.e. QVector<int> is different class from QVector<double>). If the function is a template, then it's a different function for each instantiation. So basically what you're asking (asuming template classes) is how can I connect a signal no matter from what QObject derived class it comes ... well, you can't, typically.

            You can have different overloads, however, or you can use QVariant. Mixing QObject and templates is rather dubious (due to the way the QObject is supposed to work) so that's why it's not had support in moc for so long ...

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            3
            • C Offline
              C Offline
              Christina123
              wrote on last edited by Christina123
              #5

              Hi, thank you for replying to my answers. The problem that I encounter is that when I try to overload the a signal function, an error occurs saying error: no matching member function for call to 'connect'. My code is below:

              signals:
                  void sendData(MyPath data); \\ user-defined data type
                  void sendData(QSize data);
              
              connect(rthread, &ReceiverThread::sendData, this, &Receiver::readData);
              

              I will very much want to use QVariant for my code, but unfortunately, I have a user-defined type which is vector<QPainterPath> and this is not supported by QVariant. So, QVariant may not be a feasible way.

              kshegunovK 1 Reply Last reply
              0
              • C Christina123

                Hi, thank you for replying to my answers. The problem that I encounter is that when I try to overload the a signal function, an error occurs saying error: no matching member function for call to 'connect'. My code is below:

                signals:
                    void sendData(MyPath data); \\ user-defined data type
                    void sendData(QSize data);
                
                connect(rthread, &ReceiverThread::sendData, this, &Receiver::readData);
                

                I will very much want to use QVariant for my code, but unfortunately, I have a user-defined type which is vector<QPainterPath> and this is not supported by QVariant. So, QVariant may not be a feasible way.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #6

                @Christina123 said in How to make a template?:

                The problem that I encounter is that when I try to overload the a signal function, an error occurs saying error: no matching member function for call to 'connect'.

                Yes, that's expected. How should the compiler know which of the two overloads it's supposed to pick? You have to guide it:

                QObject::connect(rthread, QOverload<TypesOfTheArguments>::of(&ReceiverThread::sendData), this, &Receiver::readData);
                

                I will very much want to use QVariant for my code, but unfortunately, I have a user-defined type which is vector<QPainterPath> and this is not supported by QVariant. So, QVariant may not be a feasible way.

                Custom types can be put in a variant, but it's involved (somewhat). They need to be copyable and constructible, and also they need to be declared as metatypes.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  Christina123
                  wrote on last edited by
                  #7

                  Hi, yes I understand my problem. So I change my code according to the code you provide connect(rthread, QOverload<MyPath,QSize>::of(&ReceiverThread::sendData), this, &Receiver::readData);, but somehow it gives me another error no matching function for call to 'of'.

                  kshegunovK 1 Reply Last reply
                  0
                  • C Christina123

                    Hi, yes I understand my problem. So I change my code according to the code you provide connect(rthread, QOverload<MyPath,QSize>::of(&ReceiverThread::sendData), this, &Receiver::readData);, but somehow it gives me another error no matching function for call to 'of'.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #8

                    @Christina123 said in How to make a template?:

                    QOverload<MyPath,QSize>

                    Choose one of the two. You select a single overload to connect, you can't have them both at the same time.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christina123
                      wrote on last edited by
                      #9

                      Hi, just want to clarify a little bit more. So, if I want to overload either signals or slots, I will have to use QOverload<>::of(...) to ensure that the moc knows which function to use. Is that correct?

                      kshegunovK 1 Reply Last reply
                      0
                      • C Christina123

                        Hi, just want to clarify a little bit more. So, if I want to overload either signals or slots, I will have to use QOverload<>::of(...) to ensure that the moc knows which function to use. Is that correct?

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #10

                        Yes, but if we are talking specifically for Qt6 then you should use the qOverload<ArgumentTypes> function, as Qt6 already requires c++17 support. For Qt5 you may need to revert to QOverload<>::of depending on your compiler.

                        https://doc-snapshots.qt.io/qt6-dev/qtglobal.html#qOverload

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        1
                        • C Offline
                          C Offline
                          Christina123
                          wrote on last edited by
                          #11

                          Thank you! This has been very helpful to me :)

                          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