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.
  • clostridium_difficileC clostridium_difficile

    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?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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
    • clostridium_difficileC Offline
      clostridium_difficileC Offline
      clostridium_difficile
      wrote on 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.

      jsulmJ KroMignonK 2 Replies Last reply
      0
      • clostridium_difficileC clostridium_difficile

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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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

        clostridium_difficileC 1 Reply Last reply
        0
        • clostridium_difficileC clostridium_difficile

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

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on 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
          • jsulmJ jsulm

            @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?

            clostridium_difficileC Offline
            clostridium_difficileC Offline
            clostridium_difficile
            wrote on 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
            	};
            }
            
            
            KroMignonK 1 Reply Last reply
            0
            • clostridium_difficileC clostridium_difficile

              @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
              	};
              }
              
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on 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
              1
              • KroMignonK KroMignon

                @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 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

                KroMignonK 1 Reply Last reply
                0
                • kshegunovK kshegunov

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

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on 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
                  0
                  • KroMignonK KroMignon

                    @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 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
                    • KroMignonK KroMignon

                      @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 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

                      • Login

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