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. [Resolved] How to Create a Pointer from Object like the example?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 2.4k Views 1 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.
  • D Offline
    D Offline
    dcbasso
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dcbasso
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • B Offline
        B Offline
        broadpeak
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dcbasso
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JSchmidt
            wrote on last edited by
            #5

            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 );
            }

            @

            1 Reply Last reply
            0
            • T Offline
              T Offline
              terenty
              wrote on last edited by
              #6

              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 :)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dcbasso
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  terenty
                  wrote on last edited by
                  #8

                  Youre wellcome :) Goodluck!

                  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