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. Getting QMetaMethod of slot member function to connect "later"
Forum Updated to NodeBB v4.3 + New Features

Getting QMetaMethod of slot member function to connect "later"

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.2k 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.
  • Y Offline
    Y Offline
    yakumoklesk
    wrote on last edited by
    #1

    Hi. I have a class that I want to use as a container to hold the receiver and the slot method of a class. So that later the first class instance will pass as a parameter to a function that will make the final connect, using whatever sender and signal to the stored receiver and slot. I am not sure how could I store the address of a member method (a slot) from a class to a QMetaMethod object to later use the overload connect method.

    At the moment I am managing to do so by making the class to be a templated class, but then the signature gets a little weird to pass the parameters, and I would simplify it as the connect method from QObject.

    Thanks a lot!

    Regards,

    David.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Can you show some pseudo code to explain what you would like to achieve ?

      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
      0
      • Y Offline
        Y Offline
        yakumoklesk
        wrote on last edited by
        #3

        I want to refactor from THIS:

        
        class ManageUserWorkflow : public QObject
        {
            Q_OBJECT;
        	enum WorkflowStep {
        		WS_STEP_1,
        		WS_STEP_2,
        		WS_STEP_3
        	}
        	enum Action {
        		A_OK,
        		A_CANCEL,
        		A_RETRY
        	}
        public:
            ManageUserWorkflow(){};
        public slot:
            void ActionHandler(){ // do Handle code }
        	FooterController* createFooter(const QList<Action>& actions)
        	{
        		FooterController* fc = new FooterController();
        		for( int i = 0; i < actionList.size(); ++i)
        		{
        			Button* b = new Button(actions.at(i));
        			connect(button, &Button::clicked, this, &ManageUserWorkflow::ActionHandler);
        			fc->addButton(b);
        		}
        	}
            DialogController* createDialog(WorkflowStep step) {
        		
        		QList<Action> actions;
                switch( step ) {
                    case WS_STEP_1:
        			{
        				dialog = new DialogController("A");
        				actions << A_OK;
        				break;
        			}
                    case WS_STEP_2:
        			{
        				dialog = new DialogController("B");
        				actions << A_OK;
        				actions << A_CANCEL;
        				break;
        			}
                    case WS_STEP_2:
        			{
        				dialog = new DialogController("C");
        				actions << A_RETRY;
        				actions << A_CANCEL;
        				break;
        			}
                }
        		
        		FooterController footer = new FooterController(actions);
        		dialog->setFooter( footer );
            }
        }
        
        

        to THIS:

        class WorkflowDialogCreator : public QObject
        {
            Q_OBJECT;
        public:
            WorkflowDialogCreator(){};
        
        	FooterController* createFooter(const QList<Action>& actions, const QList<QPair<QObject*, QMetaMethod*>> actionHandlers)
        	{
        		FooterController* fc = new FooterController();
        		for( int i = 0; i < actionList.size(); ++i)
        		{
        			Button* b = new Button(actions.at(i));
        			connect(button, &Button::clicked, actionHandlers.at(i).first, actionHandlers.at(i).first.second);
        			fc->addButton(b);
        		}
        	}
        }
        
        class ManageUserWorkflow : public QObject
            Q_OBJECT;
        	enum WorkflowStep {
        		WS_STEP_1,
        		WS_STEP_2,
        		WS_STEP_3
        	}
        	enum Action {
        		A_OK,
        		A_CANCEL,
        		A_RETRY
        	}
        public:
            ManageUserWorkflow(WorkflowDialogCreator* dc) :
        	dc_(dc){};
        public slot:
            void ActionHandler(){ // do Handle code }
        	FooterController* createFooter(const QList<Action>& actions)
        	{
        		FooterController* fc = new FooterController();
        		for( int i = 0; i < actionList.size(); ++i)
        		{
        			Button* b = new Button(actions.at(i));
        			connect(button, &Button::clicked, this, &ManageUserWorkflow::ActionHandler);
        			fc->addButton(b);
        		}
        	}
            DialogController* createDialog(WorkflowStep step) {
        		
        		QList<Action> actions;
        		QList<QPair<QObject*, QMetaMethod*>> actionHandlers
                switch( step ) {
                    case WS_STEP_1:
        			{
        				dialog = new DialogController("A");
        				actions << A_OK;
        				actionHandlers << QPair(this, &ManageUserWorkflow::ActionHandler);
        				break;
        			}
                    case WS_STEP_2:
        			{
        				dialog = new DialogController("B");
        				actions << A_OK;
        				actions << A_CANCEL;
        				actionHandlers << QPair(this, &ManageUserWorkflow::ActionHandler);
        				actionHandlers << QPair(this, &ManageUserWorkflow::ActionHandler);
        				break;
        			}
                    case WS_STEP_2:
        			{
        				dialog = new DialogController("C");
        				actions << A_RETRY;
        				actions << A_CANCEL;
        				actionHandlers << QPair(this, &ManageUserWorkflow::ActionHandler);
        				actionHandlers << QPair(this, &ManageUserWorkflow::ActionHandler);
        				break;
        			}
                }
        		
        		FooterController footer = dc_.createFooter(actions, actionHandlers);
        		dialog->setFooter( footer );
            }
        	
        	WorkflowDialogCreator* dc_;
        }
        

        Please forget constant correctness and other things. I made it fast in purpose. Also take into account this is a simplification of what the real code is.

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yakumoklesk
          wrote on last edited by
          #4

          The most similar solution of what I think I need found it here:
          https://stackoverflow.com/questions/31836199/how-to-store-qt-signal-slot-names-to-a-data-structure

          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