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. QObject::connect error: function is used but not defined in this translation unit...
Forum Updated to NodeBB v4.3 + New Features

QObject::connect error: function is used but not defined in this translation unit...

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 3.5k 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
    clostridium_difficile
    wrote on 17 Dec 2019, 10:57 last edited by
    #1

    I try to connect signal in MainWindow class with slot in GraphicsScene, but during compilation occurs an error: function 'GraphicsScene::ToolSelected' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage. Here is simplified code:

    #include <QtWidgets>
    #include "Globals.h"
    #include "ImageLayer.h"
    
    class GraphicsScene : public QGraphicsScene
    {
    Q_OBJECT
    
    	friend class MainWindow;
    
    public slots:
    	void ToolSelected(ActiveTool activeTool);
    
    };
    
    #include <QtWidgets>
    #include "Canvas.h"
    #include "LayerPreview.h"
    #include "Globals.h"
    #include "GraphicsScene.h"
    
    namespace Ui
    {
    	class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    
    signals:
    	void ToolSelected(ActiveTool activeTool);
    };
    

    Some MainWindow's function:

    ...
    graphicsScene = QSharedPointer<GraphicsScene>(new GraphicsScene(this));
    graphicsScene->setSceneRect(0, 0, width, height);
    connect(this, &MainWindow::ToolSelected, graphicsScene, &GraphicsScene::ToolSelected);
    ...
    

    How to fix it?

    J 1 Reply Last reply 17 Dec 2019, 10:59
    0
    • C clostridium_difficile
      17 Dec 2019, 10:57

      I try to connect signal in MainWindow class with slot in GraphicsScene, but during compilation occurs an error: function 'GraphicsScene::ToolSelected' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage. Here is simplified code:

      #include <QtWidgets>
      #include "Globals.h"
      #include "ImageLayer.h"
      
      class GraphicsScene : public QGraphicsScene
      {
      Q_OBJECT
      
      	friend class MainWindow;
      
      public slots:
      	void ToolSelected(ActiveTool activeTool);
      
      };
      
      #include <QtWidgets>
      #include "Canvas.h"
      #include "LayerPreview.h"
      #include "Globals.h"
      #include "GraphicsScene.h"
      
      namespace Ui
      {
      	class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
      Q_OBJECT
      
      signals:
      	void ToolSelected(ActiveTool activeTool);
      };
      

      Some MainWindow's function:

      ...
      graphicsScene = QSharedPointer<GraphicsScene>(new GraphicsScene(this));
      graphicsScene->setSceneRect(0, 0, width, height);
      connect(this, &MainWindow::ToolSelected, graphicsScene, &GraphicsScene::ToolSelected);
      ...
      

      How to fix it?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Dec 2019, 10:59 last edited by jsulm
      #2

      @clostridium_difficile said in QObject::connect error: function is used but not defined in this translation unit...:

      How to fix it?

      Define void ToolSelected(ActiveTool activeTool)
      So, do you have

      void GraphicsScene::ToolSelected(ActiveTool activeTool)
      {
      }
      

      somewhere?

      Also, why do you define MainWindow as friend in GraphicsScene?

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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        clostridium_difficile
        wrote on 17 Dec 2019, 11:21 last edited by
        #3

        It is defined already. Error still occurs. MainWindow is friend of GraphicsScene since... well, it needs access to private member functions of GraphicsScene.

        J K 2 Replies Last reply 17 Dec 2019, 11:23
        0
        • C clostridium_difficile
          17 Dec 2019, 11:21

          It is defined already. Error still occurs. MainWindow is friend of GraphicsScene since... well, it needs access to private member functions of GraphicsScene.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 Dec 2019, 11:23 last edited by
          #4

          @clostridium_difficile said in QObject::connect error: function is used but not defined in this translation unit...:

          It is defined already

          Can you show the definition? And do you actually build/link the cpp files where the definition is?

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

          C 1 Reply Last reply 17 Dec 2019, 11:32
          0
          • C clostridium_difficile
            17 Dec 2019, 11:21

            It is defined already. Error still occurs. MainWindow is friend of GraphicsScene since... well, it needs access to private member functions of GraphicsScene.

            K Offline
            K Offline
            KroMignon
            wrote on 17 Dec 2019, 11:26 last edited by KroMignon
            #5

            @clostridium_difficile Maybe a silly question, but is ActiveTool a registered type?

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0
            • J jsulm
              17 Dec 2019, 11:23

              @clostridium_difficile said in QObject::connect error: function is used but not defined in this translation unit...:

              It is defined already

              Can you show the definition? And do you actually build/link the cpp files where the definition is?

              C Offline
              C Offline
              clostridium_difficile
              wrote on 17 Dec 2019, 11:32 last edited by
              #6

              @KroMignon

              Maybe a silly question, but is ActiveTool a registered type?

              What does it exacly mean? ActiveTool's declaration is in file "Globals.h":

              #pragma once
              
              namespace
              {
              	enum class ActiveTool
              	{
              		None,
              		PenTool,
              		MoveTool
              	};
              }
              
              
              K 1 Reply Last reply 17 Dec 2019, 12:32
              0
              • C clostridium_difficile
                17 Dec 2019, 11:32

                @KroMignon

                Maybe a silly question, but is ActiveTool a registered type?

                What does it exacly mean? ActiveTool's declaration is in file "Globals.h":

                #pragma once
                
                namespace
                {
                	enum class ActiveTool
                	{
                		None,
                		PenTool,
                		MoveTool
                	};
                }
                
                
                K Offline
                K Offline
                KroMignon
                wrote on 17 Dec 2019, 12:32 last edited by
                #7

                @clostridium_difficile said in QObject::connect error: function is used but not defined in this translation unit...:

                What does it exacly mean? ActiveTool's declaration is in file "Globals.h":

                I you want to use QObject::connect() all arguments type must be registred to Qt with qRegisterMetaType<>()
                A far as I know, this is mandatory.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                kshegunovK 1 Reply Last reply 18 Dec 2019, 09:02
                1
                • K KroMignon
                  17 Dec 2019, 12:32

                  @clostridium_difficile said in QObject::connect error: function is used but not defined in this translation unit...:

                  What does it exacly mean? ActiveTool's declaration is in file "Globals.h":

                  I you want to use QObject::connect() all arguments type must be registred to Qt with qRegisterMetaType<>()
                  A far as I know, this is mandatory.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 18 Dec 2019, 09:02 last edited by
                  #8

                  @KroMignon said in QObject::connect error: function is used but not defined in this translation unit...:

                  A far as I know, this is mandatory.

                  For queued connections, not for direct connections.

                  @clostridium_difficile said in QObject::connect error: function is used but not defined in this translation unit...:

                  What does it exacly mean? ActiveTool's declaration is in file "Globals.h":

                  Remove the anonymous namespace, and try again.

                  Read and abide by the Qt Code of Conduct

                  K 1 Reply Last reply 18 Dec 2019, 09:11
                  0
                  • kshegunovK kshegunov
                    18 Dec 2019, 09:02

                    @KroMignon said in QObject::connect error: function is used but not defined in this translation unit...:

                    A far as I know, this is mandatory.

                    For queued connections, not for direct connections.

                    @clostridium_difficile said in QObject::connect error: function is used but not defined in this translation unit...:

                    What does it exacly mean? ActiveTool's declaration is in file "Globals.h":

                    Remove the anonymous namespace, and try again.

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 18 Dec 2019, 09:11 last edited by
                    #9

                    @kshegunov said in QObject::connect error: function is used but not defined in this translation unit...:

                    For queued connections, not for direct connections.

                    Yes, but as far as I can see, the connect is not forced as Qt::DirectConnection.

                    If you don't want to have trouble, my suggestion would be to change the function signature to int and cast the int to your enum inside the function:

                    class MainWindow : public QMainWindow
                    {
                    Q_OBJECT
                    
                    signals:
                    	void ToolSelected(int);
                    };
                    
                    void GraphicsScene::ToolSelected(int activeTool)
                    {
                        ActiveTool at = ActiveTool(activeTool);
                        ...
                    }
                    

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    kshegunovK JKSHJ 2 Replies Last reply 18 Dec 2019, 09:20
                    0
                    • K KroMignon
                      18 Dec 2019, 09:11

                      @kshegunov said in QObject::connect error: function is used but not defined in this translation unit...:

                      For queued connections, not for direct connections.

                      Yes, but as far as I can see, the connect is not forced as Qt::DirectConnection.

                      If you don't want to have trouble, my suggestion would be to change the function signature to int and cast the int to your enum inside the function:

                      class MainWindow : public QMainWindow
                      {
                      Q_OBJECT
                      
                      signals:
                      	void ToolSelected(int);
                      };
                      
                      void GraphicsScene::ToolSelected(int activeTool)
                      {
                          ActiveTool at = ActiveTool(activeTool);
                          ...
                      }
                      
                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 18 Dec 2019, 09:20 last edited by
                      #10

                      @KroMignon said in QObject::connect error: function is used but not defined in this translation unit...:

                      Yes, but as far as I can see, the connect is not forced as Qt::DirectConnection.

                      True enough. However the connect is between widgets, and these always must live in the main thread, so the connection is a direct one.

                      If you don't want to have trouble, my suggestion would be to change the function signature to int and cast the int to your enum inside the function:

                      I disagree. This breaks type-safety for no good reason.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      3
                      • K KroMignon
                        18 Dec 2019, 09:11

                        @kshegunov said in QObject::connect error: function is used but not defined in this translation unit...:

                        For queued connections, not for direct connections.

                        Yes, but as far as I can see, the connect is not forced as Qt::DirectConnection.

                        If you don't want to have trouble, my suggestion would be to change the function signature to int and cast the int to your enum inside the function:

                        class MainWindow : public QMainWindow
                        {
                        Q_OBJECT
                        
                        signals:
                        	void ToolSelected(int);
                        };
                        
                        void GraphicsScene::ToolSelected(int activeTool)
                        {
                            ActiveTool at = ActiveTool(activeTool);
                            ...
                        }
                        
                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on 19 Dec 2019, 00:00 last edited by JKSH
                        #11

                        @KroMignon said in QObject::connect error: function is used but not defined in this translation unit...:

                        as far as I can see, the connect is not forced as Qt::DirectConnection

                        Qt::DirectConnection is used by default when connecting two objects that live in the same thread: https://doc.qt.io/qt-5/qt.html#ConnectionType-enum

                        my suggestion would be to change the function signature to int and cast the int to your enum inside the function

                        void GraphicsScene::ToolSelected(int activeTool)
                        {
                            ActiveTool at = ActiveTool(activeTool);
                            ...
                        }
                        

                        I would not do this, for many reasons:

                        • It breaks type safety as @kshegunov said. In other words, it defeats the whole purpose of enum class which is to improve type safety in enums.
                        • It is good practice to avoid C-style casts whenever possible. Let's take extra care to avoid introducing bad practices to new C++ programmers, especially when more robust alternatives exist.
                        • There is a solution that does not require breaking type safety, does not require casting, and ultimately cleans up the code: Just remove the namespace as @kshegunov said. It is a good idea to remove the namespace anyway because anonymous namespaces should usually not be used in headers.

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        4

                        1/11

                        17 Dec 2019, 10:57

                        • 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