Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to change ui something from different class

    General and Desktop
    2
    13
    1205
    Loading More Posts
    • 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.
    • B
      beqa last edited by A Former User

      While certain function finish, I want to call different class's function , in which i update ui data, but nothing change. When i call this function from this class it works.

      mrjj 1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion @beqa last edited by

        @beqa
        Hi
        Can you show some code?

        The normal way is to ask the other window to update a widget.
        Or use signals and slots so "when certain function finish" it emit a signal
        that you have connected to a slot in the other class. this slot then
        updates.

        1 Reply Last reply Reply Quote 0
        • B
          beqa last edited by

          Hi

          void Tunnelqtworking::on_actionResidual_triggered()

          {

          dif.Residual();   // solving
          
          ResidualForm res;     // different class object 
          res.update_data();    // update this data
          

          }

          void ResidualForm::update_data() // this update_data()
          {
          ....

          ui.lcdNumber->display(r1);
          ui.lcdNumber->update();
          

          }

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @beqa last edited by mrjj

            @beqa
            Well, ResidualForm res; is a local variable and it will deleted as soon as
            on_actionResidual_triggered ends so it only be shown on screen for a very short time.

            Try to new it

            ResidualForm *res= new ResidualForm(this); // use this to give it a parent, else you leak
            res->update_data(); // update this data
            res->show();

            Also is ResidualForm ment to be a Dialog and popup or how was it planned to be shown ?
            You are not inserting it into anything.

            1 Reply Last reply Reply Quote 0
            • B
              beqa last edited by

              I have tried signal and slot, but has not worked.
              I emit like this

              void Tunnelqtworking::on_actionResidual_triggered()
              {
              dif.Residual(); // solving

              ResidualForm res; // different class object
              emit update_data(5); // I figured out ,that it needs a parameter
              }
              but in ResidualForm i can't create Tunnelqtworking object, its big and give me unclear errors

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @beqa last edited by

                @beqa

                What should ResidualForm res; do ?
                It will die there.
                So this can never work if its any kind of window.

                You want it to popup there ?

                1 Reply Last reply Reply Quote 0
                • B
                  beqa last edited by

                  I have tabWidget and add this widget
                  ui.tabWidget->addTab(new ResidualForm(), "Residual");

                  1 Reply Last reply Reply Quote 0
                  • B
                    beqa last edited by

                    I take ResidualForm *res in my .h;
                    and definit res = new ResidualForm(this);
                    but nothing change

                    1 Reply Last reply Reply Quote 0
                    • B
                      beqa last edited by

                      i try on debug and it enter ordinary this update_data function, but dont change ui element. Seems, ui has restriction from outside

                      mrjj 1 Reply Last reply Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion @beqa last edited by

                        @beqa
                        well in this case, you should NOT make a new one. you should
                        use the one you insert.

                        ui.tabWidget->addTab(new ResidualForm(), "Residual");

                        --->

                        ResidualForm *res; // THIS IN H

                        and u add it
                        res=new ResidualForm()
                        ui.tabWidget->addTab(res, "Residual");

                        so its the same.
                        res->update_data() is for the same shown on screen.

                        Do NOT create new one as its not related to the one shown on screen.

                        So the error is. You insert one , you see on screen.
                        Then you create a SECOND one that you update but its never shown on screen.

                        1 Reply Last reply Reply Quote 2
                        • B
                          beqa last edited by

                          THANK YOU, YOU ARE WELCOME. ITS WORK.
                          Differently, I wonder is it possible to change different class's ui?

                          mrjj 1 Reply Last reply Reply Quote 1
                          • mrjj
                            mrjj Lifetime Qt Champion @beqa last edited by

                            @beqa said in How to change ui something from different class:

                            , I wonder is it possible to change different class's ui?

                            They are private for the reason, that one should not directly access another's Windows
                            widgets.
                            But you can move it to public and you can access ui-> from outside but this leads
                            to bad code and not a good idea.
                            Its much better to provide function the outside can use
                            Like your update_data

                            If you allow the outside to know the inner details of other classes, you get spaghetti code and
                            a little change will result in many other changes in rest of program.

                            1 Reply Last reply Reply Quote 2
                            • B
                              beqa last edited by

                              thanks, you are very Helpfull

                              1 Reply Last reply Reply Quote 1
                              • First post
                                Last post