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. trouble connecting private slot to a signal
Forum Updated to NodeBB v4.3 + New Features

trouble connecting private slot to a signal

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 388 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.
  • M Offline
    M Offline
    mono
    wrote on last edited by
    #1

    Hello,
    I am having trouble connecting my slot to a signal that I want, I might be making some beginner mistake (since I am a beginner) but I can't figure it out even after searching on google for quite a while. Thank you for any help

    header file:

    private:
        QPushButton *btn_search;
    private slots:
        void some_function();
    };
    

    in my window.cpp file I am trying to connect the slot to a signal from the btn_search:

    window::window(QWidget *parent)
        : QWidget{parent}
    {
    btn_search = new QPushButton("Search", this);
    connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));
    }
    
    void window::some_function()
    {
        /*do something*/
    }
    

    I am not getting any errors, but the terminal says this:
    QObject::connect: Cannot connect (nullptr)::clicked() to window::combo_by_attribute()

    and the button obviously doesn't do anything. I have a lot of other stuff in the program, that work perfectly fine when I run the project. Why I can't connect these two, am I missing something? Thank you

    Pl45m4P T 2 Replies Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Your example does not match the output - btn_search is a nullptr when you call connect()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • M mono

        Hello,
        I am having trouble connecting my slot to a signal that I want, I might be making some beginner mistake (since I am a beginner) but I can't figure it out even after searching on google for quite a while. Thank you for any help

        header file:

        private:
            QPushButton *btn_search;
        private slots:
            void some_function();
        };
        

        in my window.cpp file I am trying to connect the slot to a signal from the btn_search:

        window::window(QWidget *parent)
            : QWidget{parent}
        {
        btn_search = new QPushButton("Search", this);
        connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));
        }
        
        void window::some_function()
        {
            /*do something*/
        }
        

        I am not getting any errors, but the terminal says this:
        QObject::connect: Cannot connect (nullptr)::clicked() to window::combo_by_attribute()

        and the button obviously doesn't do anything. I have a lot of other stuff in the program, that work perfectly fine when I run the project. Why I can't connect these two, am I missing something? Thank you

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @mono said in trouble connecting private slot to a signal:

        btn_search = new QPushButton("Search", this);
        connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));
        

        The error message looks like your actual code (not the one you show) has these two lines flipped, like:

        connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));
        btn_search = new QPushButton("Search", this);
        

        Also, better switch to the current standard way to establish a connection:

        connect(btn_search, &QPushButton::clicked, this, &window::some_function);
        

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        M 1 Reply Last reply
        1
        • M mono

          Hello,
          I am having trouble connecting my slot to a signal that I want, I might be making some beginner mistake (since I am a beginner) but I can't figure it out even after searching on google for quite a while. Thank you for any help

          header file:

          private:
              QPushButton *btn_search;
          private slots:
              void some_function();
          };
          

          in my window.cpp file I am trying to connect the slot to a signal from the btn_search:

          window::window(QWidget *parent)
              : QWidget{parent}
          {
          btn_search = new QPushButton("Search", this);
          connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));
          }
          
          void window::some_function()
          {
              /*do something*/
          }
          

          I am not getting any errors, but the terminal says this:
          QObject::connect: Cannot connect (nullptr)::clicked() to window::combo_by_attribute()

          and the button obviously doesn't do anything. I have a lot of other stuff in the program, that work perfectly fine when I run the project. Why I can't connect these two, am I missing something? Thank you

          T Offline
          T Offline
          TheoSys
          wrote on last edited by
          #4

          @mono I suggest to change your code like this:

          window::window(QWidget *parent)
              : QWidget{parent}
          {
             btn_search = new QPushButton("Search", this);
             connect(btn_search, &QPushButton::clicked, this, &window::some_function);
          }
          
          void window::some_function(bool checked)
          {
              /*do something*/
          }
          
          1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @mono said in trouble connecting private slot to a signal:

            btn_search = new QPushButton("Search", this);
            connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));
            

            The error message looks like your actual code (not the one you show) has these two lines flipped, like:

            connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));
            btn_search = new QPushButton("Search", this);
            

            Also, better switch to the current standard way to establish a connection:

            connect(btn_search, &QPushButton::clicked, this, &window::some_function);
            
            M Offline
            M Offline
            mono
            wrote on last edited by
            #5

            @Pl45m4 Thank you, it was the problem of flipping those two, so indeed a rookie mistake.
            I am using some older books to learn Qt, so they use old notation. My projects worked fine every time, is it a problem that I am using that syntax to connect signals and slots? Thanks

            SGaistS 1 Reply Last reply
            0
            • M mono has marked this topic as solved on
            • M mono has marked this topic as unsolved on
            • M mono has marked this topic as solved on
            • M mono

              @Pl45m4 Thank you, it was the problem of flipping those two, so indeed a rookie mistake.
              I am using some older books to learn Qt, so they use old notation. My projects worked fine every time, is it a problem that I am using that syntax to connect signals and slots? Thanks

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mono not a problem per se but you are losing on compiler generated error vs runtime debug output.

              One of the advantages is that if you modify a signal or a slot and forget to update the counterpart accordingly, you will know something is off at compile time rather than maybe missing it in logs of your application.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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