[Resolved] How to Create a Pointer from Object like the example?



  • People I have a problem!
    I have my mainwindow of application that have some menus. My application read some data from serial port and now my client wont something like this:

    • If the read data on serial port is "00 FF" the application will need to open automatically a Screen! But the class that read the data from usb don't have any reference to MainWindow Object!

    How can I make something like this?
    My first idea is to use my singleton class to make a pointer to MainWindow, it really will work, but the MainWindow is created as:

    @
    int main(int argc, char *argv[])
    {
    MainWindow w;
    w.show();
    return a.exec();
    }
    @

    How can I make a pointer from the object "w"?
    Or how can I get the pointer of this object in his constructor?

    Thanks, any other good idea is welcome!



  • Maybe I can use the Qt Connectors to solve that! It's another alternative!
    But I'm still trying to use the Pointer!



  • This way?:
    @
    MainWindow* mw = new MainWindow();
    mw->show();
    ...
    ...
    ...
    delete mw;
    @



  • If I already have the object!
    I don't like to change this code right know...



  • You either have to change code in your main and pass somehow/somewhere a reference to your object
    @
    MainWindow w;
    ...
    ...
    ...
    MainWindow * winPointer = &w; // optional...
    ...
    something.doSomething( winPointer ); // ...or right away .doSomething( &w );
    @

    or, another way could be, you pass the this pointer inside your MainWindow constructor to some object which will do somthing with you mainwindow
    @

    MainWindow::MainWindow(...) : ...
    {
    this->myPointerManager = new PointerManager(); //object which does something with your mainwindow pointer
    this->myPointerManager->setWindowPointer( this );
    }

    @



  • Hi! Unless I get the whole picture wrong, but why dont you just use signal/slot connection for this?
    Your class, that reads data from usb, in “00 FF” case, could simply emit stateOOFF() signal which you should connect to some mainwindow slot which in turn would automatically open the screen.
    P.S. I think its the way blessed by Qt :)



  • That's what I do Terenty! Connectors in Qt Rocks! Sometimes I forgot him, not this time!
    But I would like to know how to create a pointer from an object, as JSchmidt shows to me!

    Thanks all!



  • Youre wellcome :) Goodluck!


Log in to reply
 

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