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. Read variables between more than 2 forms
Qt 6.11 is out! See what's new in the release blog

Read variables between more than 2 forms

Scheduled Pinned Locked Moved General and Desktop
25 Posts 2 Posters 7.7k 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.
  • A Offline
    A Offline
    avmg
    wrote on last edited by
    #16

    Solved!

    The problem was that i was creating the instance on the constructor like this:

    Form1* form1 = new Form1;

    instead:

    form1 = new Form1();

    Stupid mistake....

    Thank you so much Chris for help me!!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      avmg
      wrote on last edited by
      #17

      Hi again,

      Finally i understood how to send data between forms and i could continue with my program. But i'm again in another trouble related with the same. I want to get a struct declared in another form. I did like this to get the struct:

      // Form1.h
      Public slots:
      mystruct return_struct(void);

      Private:
      struct mystruct{
      ...
      }

      mystruct new_mystruct;

      // Form1.cpp
      mystruct return_struct(void){
      return new_mystruct;
      }

      I've declared new_mystruct in Form1.h to have access to this data on every functions of this form. The problem is that it says in the Form1.h return_struct function:

      'mystruct' does not name a type.

      I think i should create new_mystruct on the Form1.cpp constructor but if i do this, i have not access to this on the functions...

      What do you recommend me?
      Thanks

      1 Reply Last reply
      0
      • Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #18

        Please use code tags when posting code (last button on the right). It's easier to read.

        You haven't posted entire code but from what I see you declared your struct in the private section of Form1. As so it is not visible to outside classes and the compiler is right - there is no 'mystruct' outside of Form1.
        You need to declare it in a public section and refer to it as mystruct when inside Form1 members and as Form1::mystruct anywhere outside.

        Btw. It's a good convention to start type names with capital letter. That way people know right away what is what.

        As for where you create an instance of it. You don't say what the struct contains but if you return a local variable like you did you don't get chance to fill it with any meaningful data. So either fill it up with something before you return or make it a class member and fill it elsewhere.

        Example:
        @
        //Form1.h
        class Form1 {
        public:
        MyStruct {
        ...
        }
        MyStruct getStruct() const;

        private:
        MyStruct structInstance;
        };

        //Form1.cpp
        Form1::MyStruct getStruct() const { return structInstance; }

        //Somewhere else
        Form1* form1 = ... //form1 is some instance of Form1
        Form1::MyStruct data = form1->getStruct();
        @
        You can use structInstance in any of the Form1 members this way.

        Edit: Oh, and it's not an error, but it's unusual for a getter function to be a slot. Slots are meant as functions that modify your object in a reaction to a signal. They're not meant to return values as there is no object to give that value to in a connect statement. Getters like yours should not be slots.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          avmg
          wrote on last edited by
          #19

          Hi Chris,

          I tried that but now the compiler says that 'structInstance' was not declared in this scope in the line:

          return structInstance;

          Ah! thanks for the advicements!

          1 Reply Last reply
          0
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #20

            Sorry, getStruct is a class member function. I missed class specifier:
            @
            Form1::MyStruct Form1::getStruct() const { return structInstance; }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              avmg
              wrote on last edited by
              #21

              Done!

              Thank you so much again!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                avmg
                wrote on last edited by
                #22

                Hi again!

                Before i ask how to get the struct from another form. Now i'm trying to set values from a struct located in another form. The question is that the struct is too to make a set function for each variable...so i was thinking on this two options, considering that in Form2 I have a private struct that I want to change from Form1.

                1- Try to send the complete struct. I declared a second struct in Form1, i modify the values and i send to Form2, once i receive there i change the values. But i don't have declared the struct in Form1 and also i think is a very ugly solution.

                2-Try to make a pointer in Form1 to the struct located in Form2 to modify it.

                Anyway both solutions are ugly, and i want to know a good way to do it.

                Thanks.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  avmg
                  wrote on last edited by
                  #23

                  I solved doing the 1st method.

                  Thanks

                  1 Reply Last reply
                  0
                  • Chris KawaC Online
                    Chris KawaC Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #24

                    You mean you have 2 structs in both forms and they look the same? Why not just declare it once outside of both classes?
                    From design standpoint if both forms are reading and writing to it then it clearly doesn't belong to any of them.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      avmg
                      wrote on last edited by
                      #25

                      I got a private struct in a Form, and if i want to change it from another Form i have to implement a public function like "set_value", but if the struct is too big and i don't want to add let's say 200 functions, i need to do something diferent. Finally i create a temporal struct in form1, after i modify the values i want, and finally i send this struct to the form2 where is declared the struct i wanted to modify at the begining.

                      If i declare outside i can't access because is private.

                      For sure there are many ways and better but this is the best i could done actually...

                      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