[SOLVED]Function pointer



  • Hi gurus!
    How i can do something like this:

    This is the class that stores data item and it has a pointer to function for some manipulation with data
    @class VariableItem
    {
    ....
    void (*SomeAction)();
    }
    @

    This is the main class that stores all data
    @class MainClass
    {
    QList<VariableItem> variableList;
    void action1();
    void action2();
    void action3();
    }
    @
    and as you guess VariableItem::SomeAction must get adress of one action from MainClass
    Using global functions solve this but i want use member methods. Maybe Qt already has solution for this cases?


  • Moderators

    Seems like a task for signal-slot mechanism.



  • Yeah
    But in some cases i need feedback.
    @class MainClass
    {
    QList<VariableItem> variableList;
    bool action1(QByteArray *, int *);
    bool action2(QByteArray *, int *);
    bool action3(QByteArray *, int *);
    }@

    edit
    Maybe something like delegates?



  • C++ "friend" mechanism can't help?



  • [quote author="qxoz" date="1350648921"]Yeah
    But in some cases i need feedback.
    @class MainClass
    {
    QList<VariableItem> variableList;
    bool action1(QByteArray *, int *);
    bool action2(QByteArray *, int *);
    bool action3(QByteArray *, int *);
    }@

    edit
    Maybe something like delegates?[/quote]

    "delegate" means in Qt world a special kind of controller.

    Signal/slot can help you beacuse the slot is a "simple" method what can return any arbitrary data.
    Maybe the signapmapper is the right choice here.



  • Some month ago i saw on this forum topic about creating Arraylist of functions, but i cant find it now. I think there was a solution for me.



  • Can you please show how get return value from signal
    @I = emit SomeSignal(); ?@
    and will program wait, on emit line, while binded slot is not finished?



  • You can get the return value if you invoke the signal through QMetaObject::invokeMethod. If you use the right connection type, the invokation is blocking.



  • Thank you!
    QMetaObject::invokeMethod is good solution.


Log in to reply
 

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