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. Call widget of mainwindow in an other class
Forum Updated to NodeBB v4.3 + New Features

Call widget of mainwindow in an other class

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 1.5k 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.
  • Cobra91151C Cobra91151

    @vale88

    Hello! I think the problem is with not initialized pointer - Ui::MainWindow *ptr;. For example try to initialize it in the Splash constructor.

    Example:

    ptr = new Ui::MainWindow();
    

    Also don't forget to delete ptr in the Splash destructor later.

    By the way, try to attach the debugger to detect the issue.

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #6

    @Cobra91151 I wrote:

    void Splash::on_pushButton_2_clicked()
    {
        ptr = new Ui::MainWindow;
    
        close();
        g.showMaximized();
        ptr->stackedWidget_3->setCurrentIndex(3);
        //emit invio_segnaleM();
    
    }
    
    

    but it crashes

    Cobra91151C 1 Reply Last reply
    0
    • ? A Former User

      @Cobra91151 I wrote:

      void Splash::on_pushButton_2_clicked()
      {
          ptr = new Ui::MainWindow;
      
          close();
          g.showMaximized();
          ptr->stackedWidget_3->setCurrentIndex(3);
          //emit invio_segnaleM();
      
      }
      
      

      but it crashes

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #7

      @vale88

      First, the problem with your code is, that every time you press the button (button click) new object generates and it create the memory leak.

      Set ptr = new Ui::MainWindow; to the Splash constructor to fix it and delete it in the destructor.

      Secondly, if it still crashes, try to debug your application with attached debugger.

      ? 1 Reply Last reply
      0
      • Cobra91151C Cobra91151

        @vale88

        First, the problem with your code is, that every time you press the button (button click) new object generates and it create the memory leak.

        Set ptr = new Ui::MainWindow; to the Splash constructor to fix it and delete it in the destructor.

        Secondly, if it still crashes, try to debug your application with attached debugger.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #8

        @Cobra91151 I tried with debug but I on't understand, it says that the inferior is...

        Cobra91151C 1 Reply Last reply
        0
        • ? A Former User

          @Cobra91151 I tried with debug but I on't understand, it says that the inferior is...

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #9

          @vale88

          What is your compiler and OS? Attach the screenshot of the issue from debugger here.

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

            Hi
            Why are you creating
            void Splash::on_pushButton_2_clicked()
            {
            ptr = new Ui::MainWindow; <<< WHY ? the g already done it
            close();
            g.showMaximized();
            // this is not good. = ptr->stackedWidget_3->setCurrentIndex(3);
            //should be
            g.SetPage(3); // SetPage(int index) being a new function that does ui->stackedWidget_3->setCurrentIndex(index);
            //emit invio_segnaleM();
            }

            ? 1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              Why are you creating
              void Splash::on_pushButton_2_clicked()
              {
              ptr = new Ui::MainWindow; <<< WHY ? the g already done it
              close();
              g.showMaximized();
              // this is not good. = ptr->stackedWidget_3->setCurrentIndex(3);
              //should be
              g.SetPage(3); // SetPage(int index) being a new function that does ui->stackedWidget_3->setCurrentIndex(index);
              //emit invio_segnaleM();
              }

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #11

              @mrjj yes but if I want to acceed to an object of the mainwindow, like a pushButton, how must I do?

              mrjjM 1 Reply Last reply
              0
              • ? A Former User

                @mrjj yes but if I want to acceed to an object of the mainwindow, like a pushButton, how must I do?

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

                @vale88
                Well you can make the UI public. Bad design.
                Or simply add public functions to MainWindow that does what you want.
                like
                g.SomethingWithButton();

                inside SomethingWithButton(), you have access to its UI and can
                do what ever you like.

                So make a few functions, that does what you need with MainWindows Widgets.

                ? 1 Reply Last reply
                1
                • mrjjM mrjj

                  @vale88
                  Well you can make the UI public. Bad design.
                  Or simply add public functions to MainWindow that does what you want.
                  like
                  g.SomethingWithButton();

                  inside SomethingWithButton(), you have access to its UI and can
                  do what ever you like.

                  So make a few functions, that does what you need with MainWindows Widgets.

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #13

                  @mrjj I used connect and it works, but nw I try in this way

                  mrjjM 1 Reply Last reply
                  0
                  • ? A Former User

                    @mrjj I used connect and it works, but nw I try in this way

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

                    @vale88
                    The good part about using functions to access the private UI in MainWindow is
                    that rest of program dont know which widgets it has.
                    So if you change something in MainWindow, you only need to change one other place
                    in the (access) function. rest of the program needs no changes.

                    ? 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @vale88
                      The good part about using functions to access the private UI in MainWindow is
                      that rest of program dont know which widgets it has.
                      So if you change something in MainWindow, you only need to change one other place
                      in the (access) function. rest of the program needs no changes.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #15

                      @mrjj thanks it works

                      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