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. Connecting to slot by string name
Forum Updated to NodeBB v4.3 + New Features

Connecting to slot by string name

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 7 Posters 7.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.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by
    #8

    result = connect(this, SIGNAL(testSignal()), this, SLOT(testSlot()));

    replace the above line and check.

    Thanks,

    Pradeep Kumar
    Qt,QML Developer

    1 Reply Last reply
    5
    • P poor_robert

      @artwaw
      of course you're right, but in this case I want to know how to connect with slot name in parenthesis so SLOT("testSlot()")); is exactly what I want to try.

      @m-sue :
      I have simplified the code:

      class MainWindow : public QObject
      {
      	Q_OBJECT
      
      public:
      	explicit MainWindow(QObject *parent = 0);
      	~MainWindow();
      
      signals:
      	void testSignal();
      
      public slots:
      	void testSlot();
      };
      
      
      #include "MainWindow.h"
      
      #include <QDebug>
      
      MainWindow::MainWindow(QObject *parent)
      {
      	bool result = false;
      	result = connect(this, SIGNAL(testSignal()), this, SLOT("testSlot()"));
      	qDebug() << result;
      }
      
      MainWindow::~MainWindow()
      {
      }
      
      void MainWindow::testSlot()
      {
      
      }
      
      

      but I still receive:
      QObject::connect: No such slot MainWindow::"testSlot()" in ../connections/MainWindow.cpp:8
      false

      Do you have any ideas?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #9

      @poor_robert said in Connecting to slot by string name:

      of course you're right, but in this case I want to know how to connect with slot name in parenthesis so SLOT("testSlot()")); is exactly what I want to try.

      Of course I may be wrong and I would welcome an opinion from someone more advanced in the subject but according to my knowledge this will not work at all.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      1
      • P poor_robert

        @artwaw
        of course you're right, but in this case I want to know how to connect with slot name in parenthesis so SLOT("testSlot()")); is exactly what I want to try.

        @m-sue :
        I have simplified the code:

        class MainWindow : public QObject
        {
        	Q_OBJECT
        
        public:
        	explicit MainWindow(QObject *parent = 0);
        	~MainWindow();
        
        signals:
        	void testSignal();
        
        public slots:
        	void testSlot();
        };
        
        
        #include "MainWindow.h"
        
        #include <QDebug>
        
        MainWindow::MainWindow(QObject *parent)
        {
        	bool result = false;
        	result = connect(this, SIGNAL(testSignal()), this, SLOT("testSlot()"));
        	qDebug() << result;
        }
        
        MainWindow::~MainWindow()
        {
        }
        
        void MainWindow::testSlot()
        {
        
        }
        
        

        but I still receive:
        QObject::connect: No such slot MainWindow::"testSlot()" in ../connections/MainWindow.cpp:8
        false

        Do you have any ideas?

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

        @poor_robert said in Connecting to slot by string name:

        I want to know how to connect with slot name in parenthesis so SLOT("testSlot()"));

        Why? Why do you want to use "?

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

        mrjjM 1 Reply Last reply
        3
        • jsulmJ jsulm

          @poor_robert said in Connecting to slot by string name:

          I want to know how to connect with slot name in parenthesis so SLOT("testSlot()"));

          Why? Why do you want to use "?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #11

          Hi
          the SLOT macro converts the function name to a string and hence using a string will not be converted correctly.

          # define SLOT(a)     qFlagLocation("1"#a QLOCATION)
          

          And to add to @jsulm , why you want as string anyway ??

          1 Reply Last reply
          2
          • P Offline
            P Offline
            poor_robert
            wrote on last edited by
            #12

            Hello @jsulm and @mrjj

            The code given above was just an example. The real reason was to use slot as callback. I have created following method and wanted to do following connection:

            void MyClass::setCallback(QObject * obj, const char* slotName)
            {
                connect(m_member, SIGNAL(updated()), obj, SLOT(slotName));
            }
            

            in my implementation obj is class derived from the QObject. I have solved the problem with help of std::function used as callbacks.
            Do you know how to solve this in Qt way?

            m.sueM jsulmJ 2 Replies Last reply
            0
            • BjornWB Offline
              BjornWB Offline
              BjornW
              wrote on last edited by BjornW
              #13

              I do not know if you can make that work; I doubt it.

              Regardless, I think it is a bad idea :). How do you intend to use this? Why? If you retrace your steps a bit and explain what you are doing you may get a good suggestion out of this, I think!

              1 Reply Last reply
              1
              • P poor_robert

                Hello @jsulm and @mrjj

                The code given above was just an example. The real reason was to use slot as callback. I have created following method and wanted to do following connection:

                void MyClass::setCallback(QObject * obj, const char* slotName)
                {
                    connect(m_member, SIGNAL(updated()), obj, SLOT(slotName));
                }
                

                in my implementation obj is class derived from the QObject. I have solved the problem with help of std::function used as callbacks.
                Do you know how to solve this in Qt way?

                m.sueM Offline
                m.sueM Offline
                m.sue
                wrote on last edited by m.sue
                #14

                @poor_robert said in Connecting to slot by string name:

                Just use the SLOT at the place where you call the function:

                void MyClass::setCallback(QObject * obj, const char* slotName)
                {
                    connect(m_member, SIGNAL(updated()), obj, slotName);
                }
                
                setCallback(myObj,SLOT(myFunction()));
                

                -Michael.

                jsulmJ 1 Reply Last reply
                5
                • P poor_robert

                  Hello @jsulm and @mrjj

                  The code given above was just an example. The real reason was to use slot as callback. I have created following method and wanted to do following connection:

                  void MyClass::setCallback(QObject * obj, const char* slotName)
                  {
                      connect(m_member, SIGNAL(updated()), obj, SLOT(slotName));
                  }
                  

                  in my implementation obj is class derived from the QObject. I have solved the problem with help of std::function used as callbacks.
                  Do you know how to solve this in Qt way?

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

                  @poor_robert Just pass SLOT(mySlot()) to setCallback :-)

                  void MyClass::setCallback(QObject * obj, const char* slotStr)
                  {
                      connect(m_member, SIGNAL(updated()), obj, slotStr);
                  }
                  
                  setCallback(obj, SLOT(mySlot()));
                  

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

                  1 Reply Last reply
                  5
                  • m.sueM m.sue

                    @poor_robert said in Connecting to slot by string name:

                    Just use the SLOT at the place where you call the function:

                    void MyClass::setCallback(QObject * obj, const char* slotName)
                    {
                        connect(m_member, SIGNAL(updated()), obj, slotName);
                    }
                    
                    setCallback(myObj,SLOT(myFunction()));
                    

                    -Michael.

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

                    @m.sue You were faster :-)

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

                    m.sueM 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @m.sue You were faster :-)

                      m.sueM Offline
                      m.sueM Offline
                      m.sue
                      wrote on last edited by
                      #17

                      Hi @jsulm

                      Just seconds :-)

                      1 Reply Last reply
                      1
                      • P Offline
                        P Offline
                        poor_robert
                        wrote on last edited by
                        #18

                        Hello @jsulm and @m-sue

                        This did the job :). Thanks for the help!

                        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