Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version
Forum Updated to NodeBB v4.3 + New Features

How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version

Scheduled Pinned Locked Moved Unsolved Game Development
26 Posts 6 Posters 5.3k Views 3 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.
  • ? A Former User

    @8Observer8 you shouldn't have to include any moc file.

    According to this page, the only case where the old syntax works and the new doesn't is when signal has less arguments than the slot (which will use default arguments).

    But frameSwapped signal and update slot both don't take any argument ... So this is definitely not the explanation ...

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

    @ankou29666 said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

    and update slot both don't take any argument

    See the "Hang on" I added above. This is the cause of the OP's error message.

    1 Reply Last reply
    0
    • 8Observer88 8Observer8

      @JonB I added Q_OBJECT and #include "main.moc" but the result is the same:

      main.cpp

      /*
      Build and run commands for CMD:
      > qmake -makefile
      > mingw32-make
      > "release/app"
      */
      
      #ifdef _WIN32
      #include <windows.h>
      extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
      extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
      #endif
      
      #include <iostream>
      
      #include <QtCore/QSize>
      #include <QtGui/QOpenGLFunctions>
      #include <QtGui/QSurfaceFormat>
      #include <QtOpenGL/QOpenGLWindow>
      #include <QtWidgets/QApplication>
      
      class Window: public QOpenGLWindow, private QOpenGLFunctions
      {
          Q_OBJECT
      public:
          Window()
          {
              resize(QSize(300, 300));
              setTitle("OpenGL 2.1, Qt6, C++");
      
              // Set format
              QSurfaceFormat format;
              format.setSamples(4);
              format.setSwapInterval(1);
              setFormat(format);
              // connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
              connect(this, &QOpenGLWindow::frameSwapped, this, &QOpenGLWindow::update);
          }
      
      private:
          void initializeGL() override
          {
              initializeOpenGLFunctions();
              glClearColor(0.2, 0.2, 0.2, 1);
          }
      
          void paintGL() override
          {
              glClear(GL_COLOR_BUFFER_BIT);
              std::cout << "paintGL" << std::endl;
          }
      };
      
      #include "main.moc"
      
      int main(int argc, char *argv[])
      {
      #ifdef _WIN32
          if (AttachConsole(ATTACH_PARENT_PROCESS))
          {
              freopen("CONOUT$", "w", stdout);
              freopen("CONOUT$", "w", stderr);
          }
      #endif
          std::cout << std::endl;
      
          QApplication app(argc, argv);
          Window w;
          w.show();
          return app.exec();
      }
      
      ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #11

      @JonB when you connect with new method, and that a signal or slot was inherited from a mother class, should you use the mother or child class name ? Or is this completely indifferent ?

      like connect(this, &QOpenGLWindow::frameSwapped, this, & QPaintDeviceWindow::update);

      as the update slot comes from QPaintDeviceWindow ... I'm not sure but I kinda remember having some trouble this kind.

      JonBJ 1 Reply Last reply
      0
      • ? A Former User

        @JonB when you connect with new method, and that a signal or slot was inherited from a mother class, should you use the mother or child class name ? Or is this completely indifferent ?

        like connect(this, &QOpenGLWindow::frameSwapped, this, & QPaintDeviceWindow::update);

        as the update slot comes from QPaintDeviceWindow ... I'm not sure but I kinda remember having some trouble this kind.

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

        @ankou29666
        I don't know, because I don't know which update() overload in the tree the OP wants to use. Your way may [well] work, I always use the qOverload() or QOverload<>::of in this case. It's useful to know anyway, because if a given class has multiple overloads of a method itself with different parameters you have to use this way anyway.

        ? 1 Reply Last reply
        0
        • JonBJ JonB

          @ankou29666
          I don't know, because I don't know which update() overload in the tree the OP wants to use. Your way may [well] work, I always use the qOverload() or QOverload<>::of in this case. It's useful to know anyway, because if a given class has multiple overloads of a method itself with different parameters you have to use this way anyway.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #13

          @JonB Ok but where would that second update() overload come from ? I've looked at the whole inheritance tree the only update slot I find comes from QPaintDeviceWindow, I haven't found any update slot anywhere else ...

          JonBJ 1 Reply Last reply
          0
          • ? A Former User

            @JonB Ok but where would that second update() overload come from ? I've looked at the whole inheritance tree the only update slot I find comes from QPaintDeviceWindow, I haven't found any update slot anywhere else ...

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

            @ankou29666
            Go to https://doc.qt.io/qt-6/qopenglwindow-members.html, press Ctrl+F, search for update. There are 3, all in QPaintDeviceWindow. With different parameters. So OP will indeed need the "overload" construct to specify which one (the one with no parameters here). BTW, the fact that only one is marked as [slot] does not affect the fact that you must specify which one for the connect().

            ? 1 Reply Last reply
            0
            • JonBJ JonB

              @ankou29666
              Go to https://doc.qt.io/qt-6/qopenglwindow-members.html, press Ctrl+F, search for update. There are 3, all in QPaintDeviceWindow. With different parameters. So OP will indeed need the "overload" construct to specify which one (the one with no parameters here). BTW, the fact that only one is marked as [slot] does not affect the fact that you must specify which one for the connect().

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #15

              @JonB what I see from QPaintDeviceWindow is there are two methods (one with QRect, the other with QRegion parameter) and only one slot (without parameter) ...

              JonBJ 1 Reply Last reply
              0
              • ? A Former User

                @JonB what I see from QPaintDeviceWindow is there are two methods (one with QRect, the other with QRegion parameter) and only one slot (without parameter) ...

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

                @ankou29666
                As I wrote:

                • There are 3 methods, all on that page.
                • Only one is marked as slot. But so what? I previously said:

                BTW, the fact that only one is marked as [slot] does not affect the fact that you must specify which one for the connect().

                That's why I wrote it :)

                ? 1 Reply Last reply
                1
                • JonBJ JonB

                  @ankou29666
                  As I wrote:

                  • There are 3 methods, all on that page.
                  • Only one is marked as slot. But so what? I previously said:

                  BTW, the fact that only one is marked as [slot] does not affect the fact that you must specify which one for the connect().

                  That's why I wrote it :)

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #17

                  @JonB ok I hadn't read your previous answer properly. And your last one gets me even more confused. And wondering then what's the point in declaring slots ... if you have to tell explicitly to which slot to connect ... when there's only one declared ...

                  Can a signal be connected to a method not declared as slot with the new syntax ? That's what I first understood from your last answer but I then had some doubts ... But when I think about it, it can connect to a lambda function which is not declared slot either ... I'm getting a little lost ...

                  JonBJ jsulmJ S 3 Replies Last reply
                  0
                  • ? A Former User

                    @JonB ok I hadn't read your previous answer properly. And your last one gets me even more confused. And wondering then what's the point in declaring slots ... if you have to tell explicitly to which slot to connect ... when there's only one declared ...

                    Can a signal be connected to a method not declared as slot with the new syntax ? That's what I first understood from your last answer but I then had some doubts ... But when I think about it, it can connect to a lambda function which is not declared slot either ... I'm getting a little lost ...

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

                    @ankou29666
                    Don't panic! You may be overthinking :)

                    I never used the old-style connect() with strings/SIGNAL/SLOT() macros. So I cannot say whether they insisted on the slot function being marked with slot.

                    Yes, with the new syntax at least you can connect() to any method/function, even if it has not been declared in the slots section. I don't know if there are any cases where it has to be marked as a slot, but certainly in the normal case it does not matter. Python/PyQt might require a slot method to be @Slot decorated, not sure, but C++/moc does not. Of course it is good practice to put slots into the class's slots: section, but you don't have to.

                    I think the same is not true of signals. You do have to put signals into the signals: section, IIRC.

                    The OP's original error message reads

                    error: no matching function for call to 'Window::connect(Window*, void (QOpenGLWindow::*)(), Window*, <unresolved overloaded function type>)'
                             connect(this, &QOpenGLWindow::frameSwapped, this, &QOpenGLWindow::update);
                    

                    Now, this is a C++ compiler error message. Whatever moc might or might not have done you can see the line being presented for compilation. It has to find a match, in a parameter to connect(), for &QOpenGLWindow::update, that's all it sees. And that is ambiguous because there are 3 public methods of QOpenGlWindow::update(...) which are candidates. Whether one is or is not declared inside slots: simply has no bearing. You need to tell it which one.

                    The OP will need

                    connect(this, &QOpenGLWindow::frameSwapped, this, qOverload<>(&QOpenGLWindow::update));
                    

                    Any parameters are specified inside the <>. Here that is empty so it matches the overload which takes no parameters.

                    In the case of the old-style code it was the SLOT(update()) which told it to pick the update() method with no parameters. Had you wished to pick one which did take parameters you would have had to specify them inside the update(...). So both ways are effectively specifying the same information, just in different formats.

                    1 Reply Last reply
                    0
                    • ? A Former User

                      @JonB ok I hadn't read your previous answer properly. And your last one gets me even more confused. And wondering then what's the point in declaring slots ... if you have to tell explicitly to which slot to connect ... when there's only one declared ...

                      Can a signal be connected to a method not declared as slot with the new syntax ? That's what I first understood from your last answer but I then had some doubts ... But when I think about it, it can connect to a lambda function which is not declared slot either ... I'm getting a little lost ...

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #19

                      @ankou29666 said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                      Can a signal be connected to a method not declared as slot with the new syntax ?

                      Yes

                      "what's the point in declaring slots" - it's just a hint for developers. Any method of a class can be a slot.

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

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #20

                        Ok I had started Qt by the times of 4.4 or so. New syntax didn't exist then. And then I didn't do any C++ until very recently. and with the old syntax it is required for a slot to be declared as such. That's what got me so troubled.
                        And this explains why the old syntax works when the new doesn't.

                        I was rather expecting the connection to be automatically matched by determining the number and types of the arguments but this is obviously not the case.

                        1 Reply Last reply
                        0
                        • ? A Former User

                          @JonB ok I hadn't read your previous answer properly. And your last one gets me even more confused. And wondering then what's the point in declaring slots ... if you have to tell explicitly to which slot to connect ... when there's only one declared ...

                          Can a signal be connected to a method not declared as slot with the new syntax ? That's what I first understood from your last answer but I then had some doubts ... But when I think about it, it can connect to a lambda function which is not declared slot either ... I'm getting a little lost ...

                          S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #21

                          @ankou29666 said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                          And wondering then what's the point in declaring slots

                          The old syntax used SIGNAL/SLOT macros to replace the functions with strings. Back then slots needed to be marked as slots because moc would only create mappings from the string to the function pointer for those methods. With the new syntax we are now using function pointers directly. So, for the connect call we don't have to qualify them as slots anymore. However, Qt also has some reflection built in. In these cases it could still make sense to declare some methods as slots. Though, to be honest, I haven't used that functionality in years...

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

                            The QML engine uses the string forms of signals, slots, and Q_INVOKABLEs. I haven't looked at the source, but suspect PyQt and PySide/Qt for Python do as well.

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

                            JonBJ 1 Reply Last reply
                            0
                            • jeremy_kJ jeremy_k

                              The QML engine uses the string forms of signals, slots, and Q_INVOKABLEs. I haven't looked at the source, but suspect PyQt and PySide/Qt for Python do as well.

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

                              @jeremy_k said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                              but suspect PyQt and PySide/Qt for Python do as well

                              I do not know how they implement "under the hood", but at least from a user perspective PyQt/PySide use a connection syntax "equivalent" to the new C++ style, referring to functions directly not through strings:

                              signallingObject.signal.connect(slotObject.slot)
                              
                              self.pushbutton.clicked.connect(self.onClicked)
                              
                              jeremy_kJ 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @jeremy_k said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                but suspect PyQt and PySide/Qt for Python do as well

                                I do not know how they implement "under the hood", but at least from a user perspective PyQt/PySide use a connection syntax "equivalent" to the new C++ style, referring to functions directly not through strings:

                                signallingObject.signal.connect(slotObject.slot)
                                
                                self.pushbutton.clicked.connect(self.onClicked)
                                
                                jeremy_kJ Offline
                                jeremy_kJ Offline
                                jeremy_k
                                wrote on last edited by
                                #24

                                @JonB said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                @jeremy_k said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                but suspect PyQt and PySide/Qt for Python do as well

                                I do not know how they implement "under the hood", but at least from a user perspective PyQt/PySide use a connection syntax "equivalent" to the new C++ style, referring to functions directly not through strings:

                                signallingObject.signal.connect(slotObject.slot)
                                
                                self.pushbutton.clicked.connect(self.onClicked)
                                

                                Under the hood is what I was referring to. The python syntax isn't (IMHO of course) relevant to a question about a C++ implementation. The availability of the interface is.

                                Drifting off topic, I don't consider the python syntax equivalent to C++ function pointers. Detection of a nonexistant signal or slot doesn't happen until the initial runtime parse, and detection of incompatible arguments in the slot is delayed until slot invocation. For example:

                                ...
                                object = QObject()
                                object.destroyed.connect(lambda x, y, z: print("QObject.destroyed() does not have 3 arguments"))
                                print("No exceptions so far")
                                del object
                                # With PyQt: TypeError: <lambda>() missing 2 required positional arguments: 'y' and 'z'
                                

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

                                JonBJ 1 Reply Last reply
                                0
                                • jeremy_kJ jeremy_k

                                  @JonB said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                  @jeremy_k said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                  but suspect PyQt and PySide/Qt for Python do as well

                                  I do not know how they implement "under the hood", but at least from a user perspective PyQt/PySide use a connection syntax "equivalent" to the new C++ style, referring to functions directly not through strings:

                                  signallingObject.signal.connect(slotObject.slot)
                                  
                                  self.pushbutton.clicked.connect(self.onClicked)
                                  

                                  Under the hood is what I was referring to. The python syntax isn't (IMHO of course) relevant to a question about a C++ implementation. The availability of the interface is.

                                  Drifting off topic, I don't consider the python syntax equivalent to C++ function pointers. Detection of a nonexistant signal or slot doesn't happen until the initial runtime parse, and detection of incompatible arguments in the slot is delayed until slot invocation. For example:

                                  ...
                                  object = QObject()
                                  object.destroyed.connect(lambda x, y, z: print("QObject.destroyed() does not have 3 arguments"))
                                  print("No exceptions so far")
                                  del object
                                  # With PyQt: TypeError: <lambda>() missing 2 required positional arguments: 'y' and 'z'
                                  
                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #25

                                  @jeremy_k said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                  Detection of a nonexistant signal or slot doesn't happen until the initial runtime parse, and detection of incompatible arguments in the slot is delayed until slot invocation.

                                  Which is how Python works compared against C++, regardless of Qt.

                                  My comment about Python signal connections was just intended as an observation, about how it works at coding time using function (or whatever) calls rather than strings. User gets a language check that signal and slot functions exists (at runtime in Python, and hopefully something at editing time too).

                                  jeremy_kJ 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @jeremy_k said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                    Detection of a nonexistant signal or slot doesn't happen until the initial runtime parse, and detection of incompatible arguments in the slot is delayed until slot invocation.

                                    Which is how Python works compared against C++, regardless of Qt.

                                    My comment about Python signal connections was just intended as an observation, about how it works at coding time using function (or whatever) calls rather than strings. User gets a language check that signal and slot functions exists (at runtime in Python, and hopefully something at editing time too).

                                    jeremy_kJ Offline
                                    jeremy_kJ Offline
                                    jeremy_k
                                    wrote on last edited by
                                    #26

                                    @JonB said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                    @jeremy_k said in How to replace `connect(this, SIGNAL(frameSwapped())...` with modern version:

                                    Detection of a nonexistant signal or slot doesn't happen until the initial runtime parse, and detection of incompatible arguments in the slot is delayed until slot invocation.

                                    Which is how Python works compared against C++, regardless of Qt.

                                    I agree. This is more like the string based connect.

                                    My comment about Python signal connections was just intended as an observation, about how it works at coding time using function (or whatever) calls rather than strings. User gets a language check that signal and slot functions exists (at runtime in Python, and hopefully something at editing time too).

                                    That's an IDE feature. My browser tells me when things that I type in a text input aren't in its dictionary. The words are still more like free strings than function pointers.

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

                                    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