Passing Object as Argument of diffrent Class to a Function



  • Hello All,
    I have a peice of code that I want to compress , I want to pass diffrent class object to a function and use Unknow object of class to access a Slot ;

    THis is my code
    @
    case 84: myhmi84->show(); qDebug()<<"opening Screen C";
    if((ClsAUpdtr->isActive()) == true){
    delete ClsAUpdtr;
    ClsBUpdtr = new QTimer();
    connect(ClsBUpdtr,SIGNAL(timeout()),myhmi84,SLOT(updtScr()));
    ClsBUpdtr->start(500);
    }else{
    if(ClsBUpdtr->isActive()){
    delete ClsBUpdtr;
    }
    ClsAUpdtr = new QTimer();
    connect(ClsAUpdtr,SIGNAL(timeout()),myhmi84,SLOT(updtScr()));
    ClsAUpdtr->start(500);
    }
    Del->start(10);
    break;
    case 50: myhmi87->show(); qDebug()<<"opening Screen C";
    if((ClsAUpdtr->isActive()) == true){
    delete ClsAUpdtr;
    ClsBUpdtr = new QTimer();
    connect(ClsBUpdtr,SIGNAL(timeout()),myhmi87,SLOT(updtScr()));
    ClsBUpdtr->start(500);
    }else{
    if(ClsBUpdtr->isActive()){
    delete ClsBUpdtr;
    }
    ClsAUpdtr = new QTimer();
    connect(ClsAUpdtr,SIGNAL(timeout()),myhmi87,SLOT(updtScr()));
    ClsAUpdtr->start(500);
    }
    Del->start(10);
    case X:

    @
    There are two cases I have written and I have 200 CASE so i want to make function in which I will pass object and will do further process;
    @
    //Class MYHMI84 *myhmi84; // In .h file
    //Class MYHMI87 *myhmi87; // In .h file

    // I want to Write like That IS it Possible
    eg:
    case 84: myhmi84->show();
    Function(myhmi84);
    break;
    case 50: myhmi87->show();
    Function(myhmi87);
    break;
    case X:




    void Class X::Function(ClassAny *obj){
    if((ClsAUpdtr->isActive()) == true){
    delete ClsAUpdtr;
    ClsBUpdtr = new QTimer();
    connect(ClsBUpdtr,SIGNAL(timeout()),obj,SLOT(updtScr()));
    ClsBUpdtr->start(500);
    }else{
    if(ClsBUpdtr->isActive()){
    delete ClsBUpdtr;
    }
    ClsAUpdtr = new QTimer();
    connect(ClsAUpdtr,SIGNAL(timeout()),obj,SLOT(updtScr()));
    ClsAUpdtr->start(500);
    }
    Del->start(10);
    }

    @

    Thanks
    Praveen Kumar



  • I know two possibilities to manage this:
    1.: You create a "parent" class that is inerited by your subclasses (200), so you can use a pointer to parent in your parameter list and downcast in your function body...

    2.: (less good method): You use a void* pointer...



  • Hi ,

    Write Now I am doing like that :
    @

    namespace Ui {
    class Offline;
    }
    class hmiscr01;
    class hmiscr02;
    class hmiscr03;
    class hmiscr04;
    class hmiscr42;
    class hmiscr43;



    /*CLASS DECLARATION/
    class Offline : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit Offline(QWidget parent = 0);
    ~Offline();
    /FRIEND CLASS
    ***/
    friend class hmiscr01;
    friend class hmiscr02;
    friend class hmiscr03;
    friend class hmiscr04;
    friend class hmiscr42;



    some timer;
    some public slots;
    some private slots:

    }
    @
    Could u suggest out of this .h File from this class I am handling all class.

    Praveen



  • Hi,
    what I mean with "subclassing" is creating classes that inherit from the main (parent) class.

    Example:
    @
    // main.h

    class Offline
    {
    private:
    //private members which are used later in EVERY of your child classes (200)
    public:
    Offline(QWidget *parent = 0);
    //public members which are used later in EVERY of your child classes (200)
    };

    class hmiscrXX : public Offline
    {
    private:
    //public members which are used in your specific child classes (XX) (200)
    public:
    hmiscrXX(QWidget parent = 0);
    //public members which are used in your specific child classes (XX)
    };@
    Your function has to look like that:
    @ //main.cpp
    void function doSomethingWithObjects(Offline
    hmiscr)
    {
    hmiscrXX* objhmiscrXX = qobject_cast<hmiscrXX*> (hmiscr); // downcasting hmiscr (which actually is a pointer to an object of type Offline) to your needed subclass hmiscrXX

    // do stuff
    }@

    Hope that gives you an imagination of how I meant it...


Log in to reply
 

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