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. Where should I connect signals with slots
Qt 6.11 is out! See what's new in the release blog

Where should I connect signals with slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 907 Views
  • 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.
  • M masa4

    Currently, I have about 10 classes in my project. I had included all other class headers to my mainwidget.h. Created objects and instantiated them in header. And in my mainwidget.cpp i am using connect() keywords for all signals and slots in project. Normally how should i do this?

    For example now I have ClassX, it emits startSignal and ClassY's startSlot will be responsible with that signal, and after doing its job it need to disconnect the signal. Maybe I can pass ClassX object to ClassY when creating objects in mainwidget, and connect them in ClassY. I am not sure that should be done in this way.

    Shortly, I am demanding for advices, how to/where to connect signals and slots properly.

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

    @masa4 said in Where should I connect signals with slots:

    Created objects and instantiated them in header

    In header?! This is usually done in cpp, not in header files.
    If ClassY needs to disconnect from the signale then yes you can pass pointer to ClassX to ClassY, so ClassY can connect and later disconnect from the ClassX signal.
    If there is no need to disconnect from signal then you can simply call connect where you're creating ClassX and ClassY instances.

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

    M 1 Reply Last reply
    0
    • jsulmJ jsulm

      @masa4 said in Where should I connect signals with slots:

      Created objects and instantiated them in header

      In header?! This is usually done in cpp, not in header files.
      If ClassY needs to disconnect from the signale then yes you can pass pointer to ClassX to ClassY, so ClassY can connect and later disconnect from the ClassX signal.
      If there is no need to disconnect from signal then you can simply call connect where you're creating ClassX and ClassY instances.

      M Offline
      M Offline
      masa4
      wrote on last edited by
      #3

      @jsulm Thanks for answer. As an additional question, is it ok too?
      mainwidget.h:

      ClassX *x;
      ClassY *y;
      

      mainwidget.cpp(constructor):

      x = new ClassX(y);
      y = new ClassY(x);
      

      And yes I need to move instantiation to .cpp.

      jsulmJ C 2 Replies Last reply
      0
      • M masa4

        @jsulm Thanks for answer. As an additional question, is it ok too?
        mainwidget.h:

        ClassX *x;
        ClassY *y;
        

        mainwidget.cpp(constructor):

        x = new ClassX(y);
        y = new ClassY(x);
        

        And yes I need to move instantiation to .cpp.

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

        @masa4 Yes, this is good.
        If you're using pointers you do not even need to include ClassX/ClassY header files in mainwindow.h. Instead use forward declaration and include the header files in mainwindow.cpp:

        // In mainwindow.h
        class ClassX; // Forward declaration for ClassX
        class ClassY; // Forward declaration for ClassY
        
        ClassX *x;
        ClassY *y;
        
        // In mainwindow.cc
        #include "classx_header_file.h"
        #include "classy_header_file.h"
        
        x = new ClassX(y);
        y = new ClassY(x);
        

        This technique makes header files smaller and helps to reduce compile time.

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

        1 Reply Last reply
        1
        • M masa4

          @jsulm Thanks for answer. As an additional question, is it ok too?
          mainwidget.h:

          ClassX *x;
          ClassY *y;
          

          mainwidget.cpp(constructor):

          x = new ClassX(y);
          y = new ClassY(x);
          

          And yes I need to move instantiation to .cpp.

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #5

          @masa4 said in Where should I connect signals with slots:

          x = new ClassX(y);
          y = new ClassY(x);
          

          In the first line the value of y is undefined

          jsulmJ M 2 Replies Last reply
          1
          • C ChrisW67

            @masa4 said in Where should I connect signals with slots:

            x = new ClassX(y);
            y = new ClassY(x);
            

            In the first line the value of y is undefined

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

            @ChrisW67 Oh, yes!
            @masa4 Why would you need to pass y pointer to x?

            x = new ClassX();
            y = new ClassY(x);
            

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

            M 1 Reply Last reply
            0
            • C ChrisW67

              @masa4 said in Where should I connect signals with slots:

              x = new ClassX(y);
              y = new ClassY(x);
              

              In the first line the value of y is undefined

              M Offline
              M Offline
              masa4
              wrote on last edited by
              #7

              @ChrisW67 So I cant pass them each other?

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @ChrisW67 Oh, yes!
                @masa4 Why would you need to pass y pointer to x?

                x = new ClassX();
                y = new ClassY(x);
                
                M Offline
                M Offline
                masa4
                wrote on last edited by
                #8

                @jsulm I want to emit a signal from X class for a slot in Y class. Y will disconnect after its process done. And I want the y object in X class because I will send another signal if Y is visible. In this situation i thought i need each object in each class.

                JonBJ jsulmJ 2 Replies Last reply
                0
                • M masa4

                  @jsulm I want to emit a signal from X class for a slot in Y class. Y will disconnect after its process done. And I want the y object in X class because I will send another signal if Y is visible. In this situation i thought i need each object in each class.

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

                  @masa4
                  It is unlikely each object should want a reference to the other object, that tends to lead to mutual recursion.

                  Slots should be connected to signals from somewhere which can "see" both the signal and the slot. This might be in the slot class, or it might be in another class which includes both signal & slot classes. What you should not do is connect to an external slot class from the signal class --- slots may know about signals, but signals should never know about slots.

                  1 Reply Last reply
                  2
                  • M masa4

                    @jsulm I want to emit a signal from X class for a slot in Y class. Y will disconnect after its process done. And I want the y object in X class because I will send another signal if Y is visible. In this situation i thought i need each object in each class.

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

                    @masa4 said in Where should I connect signals with slots:

                    And I want the y object in X class because I will send another signal if Y is visible

                    Who will emit this other signal? X or Y?
                    In general you should avoid such circular dependencies - this is bad design.

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

                    M 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @masa4 said in Where should I connect signals with slots:

                      And I want the y object in X class because I will send another signal if Y is visible

                      Who will emit this other signal? X or Y?
                      In general you should avoid such circular dependencies - this is bad design.

                      M Offline
                      M Offline
                      masa4
                      wrote on last edited by
                      #11

                      @jsulm X emits the another signal but nevermind, I changed the codes so not need for passing them each other now.

                      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