Qt Signal, std::function connection/reconnection at later date.



  • Hey

    I'm trying to figure out how I can "update" my signals depending on their parent.

    Esentially I have 2 QToolBar(A and B) widgets and 2 QGraphicsView(A and B), Each widget controls their own views A A and B B.

    Now I have implemented a drag function to allow for the QToolButton's /Actions to be dragged from 1 toolBar to another.

    However if I am to drag buttonX from toolbarA to toolbarB & press it, the action still get sends to toolbarA. So I figured, I can store the obj ptr and function ptr during function creation & when I do a drop event and I finish reparrenting, I could call for disconnect() and then connect() again using new objPtr. As the functionPtr remain thesame all I have to do is reuse it from header...

    So my question is, how can I create the constructor/member object to properly store the data ?

    code of where I am at:

    myAction.h

    class myAction :public QAction{
        std::function<void()> mFunction;
        QObject *mObjPtr;
      public:
      myAction(QString text, QObject *objPtr, std::function<void()> functionPtr);
        void reconnect(Object *objPtr);
    
    }
    

    myAction.cpp

    myAction::myAction(QString text, QObject *objPtr, std::function<void()> functionPtr) : mObjPtr(objPtr), mFunction(functionPtr) {
        connect(this, &QAction::triggered, objPtr, &mFunction);
    }
    
        void myAction::reconnect(Object *objPtr){
        disconnect(this, &QAction::triggered, mObjPtr, &mFunction);
        connect(this, &QAction::triggered, objPtr, &mFunction);
    mObjPtr=objPtr;
    }
    
    
    

    Usage : addAction(new icAction("action a", this, &myToolBar::printTest));

    Any help would be awesome :- )


  • Lifetime Qt Champion

    Hi,

    Something is not clear, why are your actions tied to one specific toolbar in the first place ?



  • Hmmm probably because I'm learning them in a probably backward way the more I use them now.... hmmmmmmmmmmmmm and I initially assumed that there is only 1 action and now Im reading that they are a bit more "universal" and I can have 1 action represented by multiple buttons mhmmmm

    But what I need is to link it to given toolbar to provide control over specific area of the app... hmm


  • Lifetime Qt Champion

    Than that makes that action even stranger because if I understand correctly, moving an action from one toolbar to another changes it area of effect which is likely something that's going to feel strange for your users.



  • Well, tricky. Imagine photoshop, you have the toolbar with brushes and so on. But in my case imagine that they instead directly adjust image. Lets say 1 button makes it bright, other contrasty. Now if user now creates another image, he can have another exclusive control panel for that iamge to do these things and if he has a custom script that he can drag on top of a panel. Now he can duplicate them and apply to another image... bit weird and tricky. Essentially I want to provide a toolbar that user can customize freely.


  • Qt Champions 2017

    @Dariusz
    Hi
    But the commands (action) works the same regardless of image ?
    So When action X triggers, its a question of which image is the current/being processed and not so much the action have to call a different function ?



  • Yes true in PS case. But in my case I will have separate "work" space for separate toolbars. And I want to allow user to move/copy tools from 1 toolbar to another. So the actions should relay on toolbar "parent" object and not their original connections.


  • Qt Champions 2017

    @Dariusz
    But its not the actual toolbar that handles the Trigger of the action so its a bit
    odd use case.
    But basically, when you move an Action, you want to disconnect it and then
    hook its trigger signal to slot in new/other work space ?

    Im not sure what you want to store in it with the std::function since it sounds like
    you can just disconnect and connect to the new slot ?



  • Hey

    Well, I need to store function, in order to reconnect it.

    I can do something like

    
    myAction::myAction(QString text, QObject *objPtr, std::function<void()> functionPtr) : mObjPtr(objPtr), mFunction(functionPtr) {
       connection =  connect(this, &QAction::triggered, objPtr, &mFunction);
    }
    
       void myAction::reconnect(Object *objPtr){
        disconnect(connection);
        connection = connect(this, &QAction::triggered, objPtr, &mFunction);
    mObjPtr=objPtr;
    }
    

    This way I disconnect from old object, and reconnect to new object, using stored function pointer/ref/something again.


  • Qt Champions 2017

    @Dariusz
    Hey
    Ok. but say you have 3 toolbars. When you move the action around, wont it need to know all functions then, for all toolbars ?
    Or do you just need to remember the last old method to reconnect to ?



  • Humh.... just last one I would think. The toolbars are of the same class/type so they have the same functions. I would move from tolballX(nameA) to tolballX(nameB) essentially. The toolbar would hold a pointer to the active workspace.
    And I would only need last method I think. The way it seems that I'm using the "actions" is to have individual instances of it per my workspace. I'm now thinking about mutliple toolbars per workspace but humhhhh then I could use that action mutliple toolbar icon linking. But I think it will be fine gotta test it out :- )


  • Qt Champions 2017

    @Dariusz
    ok. i think i get it even its a bit different use case that normally.
    Happy programming then.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.