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. Reaching a pointer from another function

Reaching a pointer from another function

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 571 Views
  • 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.
  • G Offline
    G Offline
    GunkutA
    wrote on last edited by GunkutA
    #1

    Hello, I am creating a new main window when user clicks to a button:

    void secDialog::on_button_logo2_clicked()
    {
       MainWindow *mainwin = new MainWindow;
       mainwin->show();
    }
    

    And in another function I want to resize and move this main window to some spesific location. Which is in here :

    void secDialog::on_pushButton_4_clicked()
    {
        QSize size = qApp->screens()[0]->size()+qApp->screens()[1]->size();
    
        int height= this->height();
        int width= this->width();
        this->setGeometry(10,10,this->width(),this->height());
        main->setGeometry(size.width()/2, 10, SecDialog->width(), SecDialog->height());
    }
    

    The problem is I don't know what I need to write in this second function instead of main. main->Setgeometry part is giving me error. How can I use the mainwindow I just created in the second fecond in here?
    PS: I am very new to both QT and C++.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Make mainwin a member pointer.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • G Offline
        G Offline
        GunkutA
        wrote on last edited by
        #3

        And how can I make it a member pointer?

        jsulmJ 1 Reply Last reply
        0
        • G GunkutA

          And how can I make it a member pointer?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @GunkutA said in Reaching a pointer from another function:

          And how can I make it a member pointer?

          Hm, like this (C++ basics):

          class secDialog {
          private:
              MainWindow *mainwin;
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          G 1 Reply Last reply
          2
          • jsulmJ jsulm

            @GunkutA said in Reaching a pointer from another function:

            And how can I make it a member pointer?

            Hm, like this (C++ basics):

            class secDialog {
            private:
                MainWindow *mainwin;
            
            G Offline
            G Offline
            GunkutA
            wrote on last edited by
            #5

            @jsulm But in this way I have to create that in the header file right? So I will only be able to create it once. But I want to create this pointer everytime user presses the button. And when I try to create it, it says unknown type name 'MainWindow'

            KroMignonK jsulmJ 2 Replies Last reply
            0
            • G GunkutA

              @jsulm But in this way I have to create that in the header file right? So I will only be able to create it once. But I want to create this pointer everytime user presses the button. And when I try to create it, it says unknown type name 'MainWindow'

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @GunkutA said in Reaching a pointer from another function:

              it says unknown type name 'MainWindow'

              Have you include the header file in which MainWindow is declared?

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              G 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @GunkutA said in Reaching a pointer from another function:

                it says unknown type name 'MainWindow'

                Have you include the header file in which MainWindow is declared?

                G Offline
                G Offline
                GunkutA
                wrote on last edited by
                #7

                @KroMignon I couldn't because as I believe we cannot include a header file into another header file.

                jsulmJ 1 Reply Last reply
                0
                • G GunkutA

                  @jsulm But in this way I have to create that in the header file right? So I will only be able to create it once. But I want to create this pointer everytime user presses the button. And when I try to create it, it says unknown type name 'MainWindow'

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @GunkutA said in Reaching a pointer from another function:

                  it says unknown type name 'MainWindow'

                  Again C++ basics.
                  Add this:

                  class MainWindow; // Add this line
                  class secDialog {
                  private:
                      MainWindow *mainwin;
                  

                  "But I want to create this pointer" - the pointer or object it is pointing to?
                  It is not clear what you want. Do you want to create 1..n instance of your MainWindow? Are you going to have more than one MainWindow instances at the same time? This would be unusual design as an application typically has only one main window.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply
                  1
                  • G GunkutA

                    @KroMignon I couldn't because as I believe we cannot include a header file into another header file.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @GunkutA said in Reaching a pointer from another function:

                    I believe we cannot include a header file into another header file

                    Of course you can as long as you don't invent circular dependencies.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @GunkutA said in Reaching a pointer from another function:

                      it says unknown type name 'MainWindow'

                      Again C++ basics.
                      Add this:

                      class MainWindow; // Add this line
                      class secDialog {
                      private:
                          MainWindow *mainwin;
                      

                      "But I want to create this pointer" - the pointer or object it is pointing to?
                      It is not clear what you want. Do you want to create 1..n instance of your MainWindow? Are you going to have more than one MainWindow instances at the same time? This would be unusual design as an application typically has only one main window.

                      G Offline
                      G Offline
                      GunkutA
                      wrote on last edited by
                      #10

                      @jsulm said in Reaching a pointer from another function:

                      Do you want to create 1..n instance of your MainWindow? 
                      

                      I want to create the MainWindow when the user presses the button. That's why I believe I shouldn't create it in the header file.

                      jsulmJ 1 Reply Last reply
                      0
                      • G GunkutA

                        @jsulm said in Reaching a pointer from another function:

                        Do you want to create 1..n instance of your MainWindow? 
                        

                        I want to create the MainWindow when the user presses the button. That's why I believe I shouldn't create it in the header file.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #11

                        @GunkutA "I want to create the MainWindow when the user presses the button" - that I already know, no need to repeat.
                        You didn't answer one of my questions: are you going to have more than one main window instances at the same time or only one? If only one then my suggestion is perfectly valid. If you want to have more than one instance at the same time then you could do

                        class MainWindow;
                        class secDialog {
                        private:
                            std::vector<MainWindow*> mainwin;
                        

                        "That's why I believe I shouldn't create it in the header file." - adding a pointer in header file does not create main window by the way...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved