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. Access to a QT instance which is declared and defined in another method
Qt 6.11 is out! See what's new in the release blog

Access to a QT instance which is declared and defined in another method

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 613 Views 2 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.
  • P Offline
    P Offline
    ppp1
    wrote on last edited by
    #1

    Hello,
    I have an QT5 Window application(QMainWindow) and an hexeditor called qhexedit (https://github.com/Simsys/qhexedit2/blob/master/src/qhexedit.cpp). The hexeditor inherits from QAbstractScrollArea.
    I have seen a code where the hexeditor was created locally in method foo and another method foo2 in the same class was able to get the instance of this hexeditor although the variable is locally to foo?

    void MainWindow::foo()
    {
       QHexEdit * hexEdit = new QHexEdit;
       setCentralWidget(hexEdit);
    }
    void MainWindow::foo2()
    {
     ???
    }
    

    Is that possible? I am not really sure. I think within the QT5 framework it is possible to get the hexeditor instance, but I don't know how. Can you give me some hints?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's nothing Qt specific. You can get your central widget back using centralWidget and then use qobject_cast to cast the pointer to your QHexEdit class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Why not simply

        class QHexEdit: // forward ( the include goes to .cpp)
        class MainWindow : public QMainWindow
        {
        ....
        private:
            Ui::MainWindow *ui;
        QHexEdit * hexEdit; /// make it a memeber
        };
        
        void MainWindow::foo()
        {
        hexEdit = new QHexEdit; // new the member 
         setCentralWidget(hexEdit);
        }
        
        void MainWindow::foo2()
        {
        hexEdit->xxxx // simply acces it or any methid of MainWindow
        }
        
        

        That way any member of MainWindow can access it.

        P 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          Why not simply

          class QHexEdit: // forward ( the include goes to .cpp)
          class MainWindow : public QMainWindow
          {
          ....
          private:
              Ui::MainWindow *ui;
          QHexEdit * hexEdit; /// make it a memeber
          };
          
          void MainWindow::foo()
          {
          hexEdit = new QHexEdit; // new the member 
           setCentralWidget(hexEdit);
          }
          
          void MainWindow::foo2()
          {
          hexEdit->xxxx // simply acces it or any methid of MainWindow
          }
          
          

          That way any member of MainWindow can access it.

          P Offline
          P Offline
          ppp1
          wrote on last edited by
          #4

          @mrjj Hi, I know that I can make hexedit to a member of the class. But I was very suprised that it is possible to get access to an object which was defined and declared locally in a function. Unfortunatly I can't remember the code of foo2 and I don't know the principle how to get rid of this problem.

          mrjjM 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            It's nothing Qt specific. You can get your central widget back using centralWidget and then use qobject_cast to cast the pointer to your QHexEdit class.

            P Offline
            P Offline
            ppp1
            wrote on last edited by
            #5

            @SGaist
            Thank you. I try to write a proof of concept.

            1 Reply Last reply
            0
            • P ppp1

              @mrjj Hi, I know that I can make hexedit to a member of the class. But I was very suprised that it is possible to get access to an object which was defined and declared locally in a function. Unfortunatly I can't remember the code of foo2 and I don't know the principle how to get rid of this problem.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @ppp1
              Well, they don't get access to the actual variable from the other function.
              That would be impossible.
              However, since the Widget is inserted into the MainWindows treelike parent/child structure , you
              can get a base pointer to any Widget and use qobject_cast to convert to your
              the actual type of Widget.
              There is also other access functions to find widgets in this structure.

              // find pushbutton named button1
              QPushButton *button = parentWidget->findChild<QPushButton *>("button1");

              P 1 Reply Last reply
              1
              • mrjjM mrjj

                @ppp1
                Well, they don't get access to the actual variable from the other function.
                That would be impossible.
                However, since the Widget is inserted into the MainWindows treelike parent/child structure , you
                can get a base pointer to any Widget and use qobject_cast to convert to your
                the actual type of Widget.
                There is also other access functions to find widgets in this structure.

                // find pushbutton named button1
                QPushButton *button = parentWidget->findChild<QPushButton *>("button1");

                P Offline
                P Offline
                ppp1
                wrote on last edited by
                #7

                @mrjj
                Thanks! I know how to get access to this object.

                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