Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to make a template?

    Qt 6
    4
    11
    418
    Loading More Posts
    • 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
      Christina123 last edited by

      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?

      JonB kshegunov 2 Replies Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @Christina123 last edited by

        @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 Reply Quote 1
        • JonB
          JonB @Christina123 last edited by

          @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 Reply Quote 0
          • jeremy_k
            jeremy_k last edited by

            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 Reply Quote 0
            • kshegunov
              kshegunov Moderators @Christina123 last edited by kshegunov

              @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 Reply Quote 3
              • C
                Christina123 last edited by 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.

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @Christina123 last edited by

                  @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 Reply Quote 1
                  • C
                    Christina123 last edited by

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

                    kshegunov 1 Reply Last reply Reply Quote 0
                    • kshegunov
                      kshegunov Moderators @Christina123 last edited by

                      @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 Reply Quote 0
                      • C
                        Christina123 last edited by

                        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?

                        kshegunov 1 Reply Last reply Reply Quote 0
                        • kshegunov
                          kshegunov Moderators @Christina123 last edited by kshegunov

                          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 Reply Quote 1
                          • C
                            Christina123 last edited by

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

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post