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?
Forum Updated to NodeBB v4.3 + New Features

How to make a template?

Scheduled Pinned Locked Moved Solved Qt 6
11 Posts 4 Posters 1.6k Views 2 Watching
  • 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 29 Apr 2021, 16:52 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?

    J K 2 Replies Last reply 29 Apr 2021, 17:24
    0
    • C Christina123
      30 Apr 2021, 15:22

      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.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 30 Apr 2021, 15:32 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
        29 Apr 2021, 16:52

        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?

        J Offline
        J Offline
        JonB
        wrote on 29 Apr 2021, 17:24 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
        • J Offline
          J Offline
          jeremy_k
          wrote on 29 Apr 2021, 19:25 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
            29 Apr 2021, 16:52

            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?

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 29 Apr 2021, 20:11 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 30 Apr 2021, 15:22 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.

              K 1 Reply Last reply 30 Apr 2021, 15:32
              0
              • C Christina123
                30 Apr 2021, 15:22

                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.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 30 Apr 2021, 15:32 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 30 Apr 2021, 15:48 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'.

                  K 1 Reply Last reply 30 Apr 2021, 16:01
                  0
                  • C Christina123
                    30 Apr 2021, 15:48

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

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 30 Apr 2021, 16:01 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 1 May 2021, 13:42 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?

                      K 1 Reply Last reply 1 May 2021, 13:54
                      0
                      • C Christina123
                        1 May 2021, 13:42

                        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?

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 1 May 2021, 13:54 last edited by kshegunov 5 Jan 2021, 13:54
                        #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 1 May 2021, 14:03 last edited by
                          #11

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

                          1 Reply Last reply
                          0

                          1/11

                          29 Apr 2021, 16:52

                          • Login

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